확장 Log4j 지원 JNDI 데이터 원본

4918 단어 log4j
log4j.properties 설정 파일:
log4j.rootLogger=INFO,db



#  log4j    JNDI   

log4j.appender.db=com.qdgswx.log4j.ds.JndiDSAppender

log4j.appender.db.jndiName=wstjndi

log4j.appender.db.BufferSize=1

log4j.appender.db.sql=insert into wx_log (openid,nsrsbh,logtype,classname,methodname,oprtime,loglevel,content) values ('%X{openid}','%X{nsrsbh}','%X{logtype}','%C','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%m')  

log4j.appender.db.layout=org.apache.log4j.PatternLayout

확장 클래스 JndiDSAppender.java
public class JndiDSAppender extends JDBCAppender {



    protected DataSource ds = null;

    protected String jndiName;



    public void setJndiName(String jndiName) {

        this.jndiName = jndiName;

    }

    

    protected synchronized void init() {

        if (ds != null) {

            return;

        }

        try {

            InitialContext context = new InitialContext();

            ds = (DataSource) context.lookup(jndiName);

        } catch (NamingException e) {

            this.errorHandler.error(e.getMessage());

        }

    }

    

     @Override  

    protected Connection getConnection() throws SQLException {  

        while(ds==null){

            init();

        }

        

        Connection conn = ds.getConnection();

        conn.setAutoCommit(true);

        

        return conn;

    }

    

    @Override  

    protected void closeConnection(Connection conn) {  

        try {  

            if(conn!=null&&!conn.isClosed()) {

                conn.close();  

            } 

        } catch (SQLException e) {  

            this.errorHandler.error(e.getMessage());  

        }  

    }  

}

좋은 웹페이지 즐겨찾기