Extended log4j supports Jndi data sources

Source: Internet
Author: User

Log4j.properties configuration file:

Log4j.rootlogger=info,db# supports Jndi data sources using log4j extensions log4j.appender.db=Com.qdgswx.log4j.ds.JndiDSAppenderlog4j.appender.db.jndiName=wstjndilog4j.appender.db.BufferSize=1Log4j.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

Extended Class Jndidsappender.java

 Public classJndidsappenderextendsJdbcappender {protectedDataSource ds =NULL; protectedString Jndiname;  Public voidsetjndiname (String jndiname) { This. Jndiname =Jndiname; }        protected synchronized voidinit () {if(ds! =NULL) {            return; }        Try{InitialContext Context=NewInitialContext (); DS=(DataSource) context.lookup (jndiname); } Catch(namingexception e) { This. Errorhandler.error (E.getmessage ()); }} @OverrideprotectedConnection getconnection ()throwsSQLException { while(ds==NULL) {init (); } Connection Conn=ds.getconnection (); Conn.setautocommit (true); returnConn; } @Overrideprotected voidCloseConnection (Connection conn) {Try {              if(conn!=NULL&&!conn.isclosed ())              {Conn.close (); }         } Catch(SQLException e) { This. Errorhandler.error (E.getmessage ()); }      }  }

Extended log4j supports Jndi data sources

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.