配置log4j写入数据库

首先按tomcat +sqlserver 数据连接池 中的方法配置数据连接池。

\WEB-INF\lib文件夹加入包msbase.jar;mssqlserver.jar;msutil.jar;log4j-1.2.8.jar

放在WEB_INF\class文件夹下的log.properties文件的内容如下

#输出到F:/home/example.log
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=F:/home/example.log
log4j.appender.A1.MaxBackeupIndex=1
log4j.appender.A1.MaxFileSize=1MB
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%-5p][%d]%n%m%n%n

#控制台输出
#log4j.logger.A2=, myAppender
#log4j.appender.A1=org.apache.log4j.ConsoleAppender
#log4j.appender.A1.layout=org.apache.log4j.PatternLayout
#log4j.appender.A1.layout.ConversionPattern= [%-5p][%d]%n%m%n%n

#写入数据库
#log4j.rootLogger=DEBUG, DATABASE 
#log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender//(root日志记录器的存储器)
#log4j.appender.DATABASE.URL=jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=cursor
#log4j.appender.DATABASE.driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
#log4j.appender.DATABASE.user=sd //用户名
#log4j.appender.DATABASE.password=
#log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('%d %c %p %m%n')//写入pubs数据库中的表LOG4J的Message字段中,内容%d(日期)%c: 日志信息所在地(类名)%p: 日志信息级别%m: 产生的日志具体信息 %n: 输出日志信息换行 
#log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
#log4j.appender.DATABASE.layout.ConversionPattern=%d %c %p %m%n

类文件中的使用方法

import org.apache.log4j.Logger;

import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Vector;

/**
* 封装DataSource
*
* @version 1.0, 2005-9-16 11:18:44
*/

public class DataSourcePackBean {
     /**
      * log4j
      */
     private Logger logger = Logger.getLogger(DataSourcePackBean.class);

//DataSourcePackBean.class日志记录器的名与类名相同;

     /**
      * 引用名
      */
     private String ref;

     /**
      * DataSource对象
      */
     private DataSource ds;

     /**
      * 保存当前DataSource对象中所有打开的Connection对象的引用。
      */
     private Vector openedConnections = new Vector();

     /**
      * 接收DataSource引用名,查找DataSource对象。
      *
      * @param ref DataSource引用名
      */
     protected DataSourcePackBean(String ref) {
         this.ref = ref;
         this.ds = lookup(ref);
     }

     public String getRef() {
         return ref;
     }

     public int getActive() {
         return openedConnections.size();
     }

     /**
      * 打开一个连接
      *
      * @param username 连接数据库的登录用户名,如果为null,表示用默认用户连接
      * @param password 连接数据库的登录密码
      * @return
      * @throws SQLException
      */
     public Connection getConnection(String username, String password) throws SQLException {
         Connection conn = (username == null) ? ds.getConnection() : ds.getConnection(username, password);
         // add to vector
         openedConnections.add(conn);
         // log
         StringBuffer log = new StringBuffer();
         log.append("Open").append(" a connection(").append(conn.hashCode()).append(") of [").append(ref).append("]\n");
         log.append(openedConnections.size()).append(" connection").append(" in use");
         logger.debug(log);
         // return
         return conn;
     }

     /**
      * 关闭一个连接
      *
      * @param conn
      * @throws SQLException
      */
     public void closeConnection(Connection conn) throws SQLException {
         conn.close();
         // remove from Vector
         openedConnections.remove(conn);
         // log
         StringBuffer log = new StringBuffer();
         log.append("Close").append(" a connection(").append(conn.hashCode()).append(") of [").append(ref).append("]\n");//添加内容到日志记录中
         log.append(openedConnections.size()).append(" connection").append(" in use");
         logger.debug(log);
     }

     /**
      * close all Connection of this DataSource
      * @throws SQLException
      */
     public void closeAll() throws SQLException {
         for (int i = 0; i < openedConnections.size(); i++) {
             ((Connection)openedConnections.get(i)).close();
         }
         // clear Vector
         openedConnections.removeAllElements();
     }

     /**
      * get a DataSource
      * @param ref the JNDI name of the DataSource
      * @return
      */
     private DataSource lookup(String ref) {
         DataSource dataSource = null;
         // 查找ref数据源对象
         try {
             Context env = new InitialContext();
             dataSource = (DataSource) env.lookup(ref);
             // 测试数据库有效性
             dataSource.getConnection().close();
             logger.debug(new StringBuffer().append("DataSource[").append(ref).append("] has been initialized successfully."));
         } catch (Exception e) {
             e.printStackTrace();
         }
         // return
         return dataSource;
     }

     public SQLRunnerIfc getSQLRunner(String username, String password) throws SQLException {
         return new SQLRunner(username, password, this);
     }

     public SQLRunnerIfc getSQLRunner() throws SQLException {
         return getSQLRunner(null, null);
     }
}

 

;