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 的子类:
- package com.hmw.log4j;
- import java.sql.Connection;
- import java.sql.SQLException;
- import org.apache.log4j.spi.ErrorCode;
- import com.codestudio.sql.PoolMan;
- /**
- * Log4j的 Appender, 通过连接池获取数据库连接
- * @author Carl He
- */
- public class MyJDBCAppender extends org.apache.log4j.jdbc.JDBCAppender {
- /**通过 PoolMan 获取数据库连接对象的 jndiName 属性*/
- protected String jndiName;
- /**数据库连接对象*/
- protected Connection connection;
- public MyJDBCAppender() {
- super();
- }
- @Override
- protected void closeConnection(Connection con) {
- try {
- if (connection != null && !connection.isClosed())
- connection.close();
- } catch (SQLException e) {
- errorHandler.error("Error closing connection", e, ErrorCode.GENERIC_FAILURE);
- }
- }
- @Override
- protected Connection getConnection() throws SQLException {
- try {
- //通过 PoolMan 获取数据库连接对象(http://nchc.dl.sourceforge.net/project/poolman/PoolMan/poolman-2.1-b1/poolman-2.1-b1.zip)
- Class.forName("com.codestudio.sql.PoolMan");
- connection= PoolMan.connect("jdbc:poolman://" + getJndiName());
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return connection;
- }
- /**
- * @return the jndiName
- */
- public String getJndiName() {
- return jndiName;
- }
- /**
- * @param jndiName the jndiName to set
- */
- public void setJndiName(String jndiName) {
- this.jndiName = jndiName;
- }
- }
log4j.properties 文件中加上如下一段代码(对此 Appender 的配置):
- log4j.appender.JDBC=com.hmw.log4j.MyJDBCAppender
- log4j.appender.JDBC.jndiName=log
- log4j.appender.JDBC.layout=org.apache.log4j.PatternLayout
- log4j.appender.JDBC.sql=INSERT INTO LOGGING (log_date, log_level, location, message) VALUES ('%d{ISO8601}', '%-5p', '%C,%L', '%m')
最后不要忘了 PoolMan 的配置文件 poolman.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <poolman>
- <management-mode>local</management-mode>
- <datasource>
- <dbname>log</dbname>
- <jndiName>log</jndiName>
- <driver>com.mysql.jdbc.Driver</driver>
- <url>jdbc:mysql://localhost:3306/test</url>
- <username>use</username>
- <password>password</password>
- <minimumSize>0</minimumSize>
- <maximumSize>10</maximumSize>
- <logFile>logs/mysql.log</logFile>
- </datasource>
- </poolman>