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 a PatternLayout
(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:
getConnection()
to pass any connection you want. Typically this is used to enable application wide connection pooling. closeConnection(Connection con)
-- if you override getConnection make sure to implement closeConnection
to handle the connection you generated. Typically this would return the connection to the pool it came from. 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 的子类:
log4j.properties 文件中加上如下一段代码(对此 Appender 的配置):
最后不要忘了 PoolMan 的配置文件 poolman.xml: