Log4j 配置数据库连接池(将日志信息保存到数据库)

    org.apache.log4j.jdbc.JDBCAppender 是利用传统的 JDBC 连接方法,这种方式连接数据库效率低下,为了解决这个问题,现在自定义一个 Log4j 的 Appender, 将数据库连接改为连接池的形式,此 Appender 继承自 org.apache.log4j.jdbc.JDBCAppender, 并运用了开源项目Poolman(可从http://nchc.dl.sourceforge.net/project/poolman/PoolMan/poolman-2.1-b1/poolman-2.1-b1.zip 下载)数据库连接池的包。


    org.apache.log4j.jdbc.JDBCAppender 的官方 API(http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/jdbc/JDBCAppender.html ) 中有这么一段话:

WARNING: This version of JDBCAppender is very likely to be completely replaced in the future. Moreoever, it does not log exceptions . The JDBCAppender provides for sending log events to a database.

Each append call adds to an ArrayList buffer. When the buffer is filled each log event is placed in a sql statement (configurable) and executed. BufferSize , db URL , User , & Password are configurable options in the standard log4j ways.

The setSql(String sql) sets the SQL statement to be used for logging -- this statement is sent to aPatternLayout (either created automaticly by the appender or added by the user). Therefore by default all the conversion patterns in PatternLayout can be used inside of the statement. (see the test cases for examples)

Overriding the getLogStatement(org.apache.log4j.spi.LoggingEvent) method allows more explicit control of the statement used for logging.

For use as a base class:

  • Override getConnection() to pass any connection you want. Typically this is used to enable application wide connection pooling.
  • Override closeConnection(Connection con) -- if you override getConnection make sure to implementcloseConnection to handle the connection you generated. Typically this would return the connection to the pool it came from.
  • Override getLogStatement(LoggingEvent event) to produce specialized or dynamic statements. The default uses the sql option value.

大概意思就是建议我们把其提供的 org.apache.log4j.jdbc.JDBCAppender 作为基类来使用,然后 Override 这三个方法: getConnection()、 closeConnection(Connection con) 和 getLogStatement(LoggingEvent event)。

下面是我写的一个 org.apache.log4j.jdbc.JDBCAppender 的子类:

 

[java]  view plain copy
 
  1. package com.hmw.log4j;  
  2. import java.sql.Connection;  
  3. import java.sql.SQLException;  
  4. import org.apache.log4j.spi.ErrorCode;  
  5. import com.codestudio.sql.PoolMan;  
  6. /** 
  7.  * Log4j的 Appender, 通过连接池获取数据库连接 
  8.  * @author Carl He 
  9.  */  
  10. public class MyJDBCAppender extends org.apache.log4j.jdbc.JDBCAppender {  
  11.     /**通过 PoolMan 获取数据库连接对象的 jndiName 属性*/  
  12.     protected String jndiName;  
  13.     /**数据库连接对象*/  
  14.     protected Connection connection;  
  15.     public MyJDBCAppender() {  
  16.         super();  
  17.     }  
  18.     @Override  
  19.     protected void closeConnection(Connection con) {  
  20.         try {  
  21.             if (connection != null && !connection.isClosed())  
  22.                 connection.close();  
  23.         } catch (SQLException e) {  
  24.             errorHandler.error("Error closing connection", e, ErrorCode.GENERIC_FAILURE);  
  25.         }  
  26.     }  
  27.     @Override  
  28.     protected Connection getConnection() throws SQLException {  
  29.         try {  
  30.             //通过 PoolMan 获取数据库连接对象(http://nchc.dl.sourceforge.net/project/poolman/PoolMan/poolman-2.1-b1/poolman-2.1-b1.zip)  
  31.             Class.forName("com.codestudio.sql.PoolMan");  
  32.             connection= PoolMan.connect("jdbc:poolman://" + getJndiName());  
  33.         } catch (Exception e) {  
  34.             System.out.println(e.getMessage());  
  35.         }  
  36.         return connection;  
  37.     }  
  38.     /** 
  39.      * @return the jndiName 
  40.      */  
  41.     public String getJndiName() {  
  42.         return jndiName;  
  43.     }  
  44.     /** 
  45.      * @param jndiName the jndiName to set 
  46.      */  
  47.     public void setJndiName(String jndiName) {  
  48.         this.jndiName = jndiName;  
  49.     }  
  50. }  

 

log4j.properties 文件中加上如下一段代码(对此 Appender 的配置):

 

[xhtml]  view plain copy
 
  1. log4j.appender.JDBC=com.hmw.log4j.MyJDBCAppender  
  2. log4j.appender.JDBC.jndiName=log  
  3. log4j.appender.JDBC.layout=org.apache.log4j.PatternLayout  
  4. log4j.appender.JDBC.sql=INSERT INTO LOGGING (log_date, log_level, location, message) VALUES ('%d{ISO8601}', '%-5p', '%C,%L', '%m')  

 

最后不要忘了 PoolMan 的配置文件 poolman.xml:

 

[xhtml]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <poolman>  
  3.     <management-mode>local</management-mode>  
  4.     <datasource>  
  5.         <dbname>log</dbname>  
  6.         <jndiName>log</jndiName>  
  7.         <driver>com.mysql.jdbc.Driver</driver>  
  8.         <url>jdbc:mysql://localhost:3306/test</url>  
  9.         <username>use</username>  
  10.         <password>password</password>  
  11.         <minimumSize>0</minimumSize>  
  12.         <maximumSize>10</maximumSize>  
  13.         <logFile>logs/mysql.log</logFile>  
  14.     </datasource>  
  15. </poolman>  

 

你可能感兴趣的:(数据库连接池)