Org. apache. log4j. jdbc. JDBCAppender uses the traditional JDBC connection method to connect to the database, which is inefficient. To solve this problem, we now customize a Log4j Appender and change the database connection to the connection pool, this Appender inherits from org. apache. log4j. jdbc. JDBCAppender, and uses the open source project Poolman (can be from the http://nchc.dl.sourceforge.net/project/poolman/PoolMan/poolman-2.1-b1/poolman-2.1-b1.zip
Download) database connection pool package.
Official API of org. apache. log4j. jdbc. JDBCAppender (
Http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/jdbc/JDBCAppender.html
) Has such a paragraph:
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 toArrayList
Buffer. When
The buffer is filled each log event is placed in a SQL statement
(Retriable) and executed.
Buffersize
,DB URL
,User
,&Password
Are
Retriable options in the standard log4j ways.
ThesetSql(String sql)
Sets the SQL statement to be
Used for logging -- this statement is sent to
PatternLayout
(Either created automaticly by
Appender or added by the user). Therefore by default all
Conversion patterns inPatternLayout
Can be used
Inside of the statement. (see the test cases for examples)
OverridinggetLogStatement(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 implement
closeConnection
To handle the connection you
Generated. Typically this wowould return the connection to
Pool it came from.
- Override
getLogStatement(LoggingEvent event)
To
Produce specialized or dynamic statements. The default uses
SQL option value.
We recommend that you use org. apache. log4j. jdbc. JDBCAppender as the base class, and then Override the three methods: getConnection (),
CloseConnection (Connection con) and getLogStatement (LoggingEvent event ).
Below is a subclass of org. apache. log4j. jdbc. JDBCAppender I wrote:
Package com. hmw. log4j; <br/> import java. SQL. connection; <br/> import java. SQL. SQLException; <br/> import org. apache. log4j. spi. errorCode; <br/> import com. codestudio. SQL. poolMan; <br/>/** <br/> * Log4j Appender, obtain the database connection through the connection pool <br/> * @ author Carl He <br/> */<br/> public class MyJDBCAppender extends org. apache. log4j. jdbc. JDBCAppender {<br/>/** use PoolMan to obtain the jndiName attribute of the database connection object */<br/> protected String jn DiName; <br/>/** database Connection object */<br/> protected connection Connection; <br/> public MyJDBCAppender () {<br/> super (); <br/>}< br/> @ Override <br/> protected void closeConnection (Connection con) {<br/> try {<br/> if (connection! = Null &&! Connection. isClosed () <br/> connection. close (); <br/>}catch (SQLException e) {<br/> errorHandler. error ("Error closing connection", e, ErrorCode. GENERIC_FAILURE); <br/>}< br/> @ Override <br/> protected Connection getConnection () throws SQLException {<br/> try {<br/> // get database connection object (http://nchc.dl.sourceforge.net/project/poolman/PoolMan/poolman-2.1-b1/poolman-2.1-b1.zip) through PoolMan <br/> Class. forName ("com. codestudio. SQL. poolMan "); <br/> connection = PoolMan. connect ("jdbc: poolman: //" + getJndiName (); <br/>}catch (Exception e) {<br/> System. out. println (e. getMessage (); <br/>}< br/> return connection; <br/>}< br/>/** <br/> * @ return the jndiName <br/> */<br/> public String getJndiName () {<br/> return jndiName; <br/>}< br/>/** <br/> * @ param jndiName the jndiName to set <br/> */<br/> public void setJndiName (String jndiName) {<br/> this. jndiName = jndiName; <br/>}< br/>}
Add the following code to the log4j. properties file (configuration of this Appender ):
Log4j. appender. JDBC = com. HMW. log4j. myjdbcappender <br/> log4j. appender. JDBC. jndiname = Log <br/> log4j. appender. JDBC. layout = org. apache. log4j. patternlayout <br/> log4j. appender. JDBC. SQL = insert into logging (log_date, log_level, location, message) values ('% d {iso8601}', '%-5',' % C, % l ', '% m ')
Do not forget the poolman configuration file.
Poolman. xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <poolman> <br/> <management-mode> local </management-mode> <br/> <datasource> <br/> <dbname> log </ dbname> <br/> <jndiName> log </jndiName> <br/> <driver> com. mysql. jdbc. driver </driver> <br/> <url> jdbc: mysql: // localhost: 3306/test </url> <br/> <username> use </username> <br/> <password> password </password> <br/> <minimumSize> 0 </minimumSize> <br/> <maximumSize> 10 </maximumSize> <br/> <logFile> logs/mysql. log </logFile> <br/> </datasource> <br/> </poolman>