<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc3</artifactId>
<version>1.12</version>
<scope>compile</scope>
</dependency>
示例代码
String driverClassName = "net.sf.log4jdbc.sql.jdbcapi.DriverSpy";
try
{
Class.forName(driverClassName);
}
catch (Exception t)
{
System.out.println(t.getStackTrace());
}
log4j属性文件
log4j.rootLogger=INFO,FILE
! Log connection open/close events and connection number dump
log4j.logger.jdbc.connection=FATAL,connection
log4j.additivity.jdbc.connection=false
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
! the appender used for the JDBC Connection open and close events
log4j.appender.connection=org.apache.log4j.FileAppender
log4j.appender.connection.File=/home/nirmala/projects/connection.log
log4j.appender.connection.Append=false
log4j.appender.connection.layout=org.apache.log4j.PatternLayout
log4j.appender.connection.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n
出现以下错误。
Exception in thread "main" java.lang.NoClassDefFoundError: Unable to find Log4j2 as default logging library. Please provide a logging library and configure a valid spyLogDelegator name in the properties file.
at net.sf.log4jdbc.log.SpyLogFactory.loadSpyLogDelegator(SpyLogFactory.java:94)
at net.sf.log4jdbc.Properties.<clinit>(Properties.java:203)
at net.sf.log4jdbc.log.SpyLogFactory.getSpyLogDelegator(SpyLogFactory.java:69)
at net.sf.log4jdbc.sql.jdbcapi.DriverSpy.<clinit>(DriverSpy.java:133)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:195)
at org.apache.commons.dbcp.Main.main(Main.java:11)