<< Gottox/socket.io-java-client · GitHub | 首页 | 向oracle中导入导出 *.csv文件 - Eolande - 博客园 >>

【log4jdbc】log4jdbc日志框架介绍_梁小坏Leo_新浪博客

配置方法

 

1.导入log4jdbcjar

JDK1.4 JDK1.5 选择 log4jdbc3-1.2.jar
JDK1.6 JDK1.7 选择 log4jdbc4-1.2.jar

 

2.导入SLF4J的jar
下载最新SLF4J
http://www.slf4j.org/download.html

下载之后将得到2个jar文件,导入项目classpath

 

3.修改你的项目的JDBC驱动

Driver Class                                             数据库类型
oracle.jdbc.driver.OracleDriver                          Older Oracle Driver
oracle.jdbc.OracleDriver                                 Newer Oracle Driver
com.sybase.jdbc2.jdbc.SybDriver                          Sybase
net.sourceforge.jtds.jdbc.Driver                         jTDS SQL Server & Sybase driver
com.microsoft.jdbc.sqlserver.SQLServerDriver             Microsoft SQL Server 2000 driver
com.microsoft.sqlserver.jdbc.SQLServerDriver             Microsoft SQL Server 2005 driver
weblogic.jdbc.sqlserver.SQLServerDriver                  Weblogic SQL Server driver
com.informix.jdbc.IfxDriver                              Informix
org.apache.derby.jdbc.ClientDriver                       Apache Derby client/server driver, aka the Java DB
org.apache.derby.jdbc.EmbeddedDriver                     Apache Derby embedded driver, aka the Java DB
com.mysql.jdbc.Driver                                    MySQL
org.postgresql.Driver                                    PostgresSQL
org.hsqldb.jdbcDriver                                    HSQLDB pure Java database
org.h2.Driver                                            H2 pure Java database

 

4.修改JDBC连接URL

比如你的URL为

jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase

那么修改为

jdbc:log4jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase

 

5.设置日志Logger在log4jdbc里面,新添加了5个Logger。

logger                 描述
jdbc.sqlonly           仅记录SQL。
jdbc.sqltiming         计时统计SQL的执行用时。
jdbc.audit             记录所有的JDBC调用(ResultSet的除外)。Log量非常大,会影响性能,建议关闭。
                       一般情况下不需要开这个设定,除非追踪一个特定的JDBC问题。
jdbc.resultset         比audit量更大,包括ResultSet对象,记录所有的JDBC记录。建议关闭
jdbc.connection        记录打开和关闭连接以及打开的连接数。用于追踪连接泄漏问题,非常有用。

 

这里给出log4j.xml和log4j.properties的例子


log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<!-- An example log4j configuration xml file for log4jdbc -->
<!-- Logging levels are:                                  -->
<!-- DEBUG < INFO < WARN < ERROR < FATAL                  -->

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="stdout-appender" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1}: %m%n"/>
    </layout>
  </appender>

  <appender name="sql-appender" class="org.apache.log4j.FileAppender">
    <param name="File" value="./logs/sql.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="-----&gt; %d{yyyy-MM-dd HH:mm:ss.SSS} &lt;%t&gt; %m%n%n"/>
    </layout>
  </appender>

  <appender name="sql-timing-appender" class="org.apache.log4j.FileAppender">
    <param name="File" value="./logs/sqltiming.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="-----&gt; %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n"/>
    </layout>
  </appender>

  <appender name="jdbc-appender" class="org.apache.log4j.FileAppender">
    <param name="File" value="./logs/jdbc.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n"/>
    </layout>
  </appender>

  <appender name="jdbc-connection" class="org.apache.log4j.FileAppender">
    <param name="File" value="./logs/connection.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n"/>
    </layout>
  </appender>

  <!--
       The Following 5 logs can be turned on and off while the server is running
       LIVE in order to trace the SQL and/or all JDBC coming out of the application.

       To turn a log on, set the level value to INFO or DEBUG (to see class name and
       line number information in the log)  The DEBUG setting is much more inefficient
       but the output is much more useful.

       To turn off JDBC logging completely, you must set all 5 logs to a level higher
       than ERROR (FATAL is suggested.)
  -->

  <!-- log SQL (pre-execution) plus exceptions caused by SQL -->
  <logger name="jdbc.sqlonly" additivity="false">
    <level value="debug"/>
    <appender-ref ref="sql-appender"/>
  </logger>

  <!-- log SQL with timing information, post execution -->
  <logger name="jdbc.sqltiming" additivity="false">
    <level value="fatal"/>
    <appender-ref ref="sql-timing-appender"/>
  </logger>

  <!-- only use the two logs below to trace ALL JDBC information,
       NOTE:  This can be very voluminous!  -->

  <!-- log all jdbc calls except ResultSet calls -->
  <logger name="jdbc.audit" additivity="false">
    <level value="fatal"/>
    <appender-ref ref="jdbc-appender"/>
  </logger>

  <!-- log the jdbc ResultSet calls -->
  <logger name="jdbc.resultset" additivity="false">
    <level value="fatal"/>
    <appender-ref ref="jdbc-appender"/>
  </logger>
 
  <!-- log connection open/close events and dump of all open connection numbers -->
  <logger name="jdbc.connection" additivity="false">
    <level value="fatal"/>
    <appender-ref ref="connection-appender"/>
  </logger>

  <!-- this log is for internal debugging of log4jdbc, itself -->
  <!-- debug logging for log4jdbc itself -->
  <logger name="log4jdbc.debug" additivity="false">
    <level value="debug"/>
    <appender-ref ref="stdout-appender"/>
  </logger>

  <!-- by default, log everything to the console with a level of WARN or higher -->
  <root>
    <level value="warn"/>
    <appender-ref ref="stdout-appender"/>
  </root>
</log4j:configuration>


log4j.properties

!==============================================================================
! log4j.properties - An example configuration properties file for log4j.
!
! Logging levels are:
! DEBUG < INFO < WARN < ERROR < FATAL
!==============================================================================

! turn on the internal log4j debugging flag so we can see what it is doing
log4j.debug=true

!==============================================================================
! JDBC API layer call logging :
! INFO shows logging, DEBUG also shows where in code the jdbc calls were made,
! setting DEBUG to true might cause minor slow-down in some environments.
! If you experience too much slowness, use INFO instead.

! Log all JDBC calls except for ResultSet calls
log4j.logger.jdbc.audit=INFO,jdbc
log4j.additivity.jdbc.audit=false

! Log only JDBC calls to ResultSet objects
log4j.logger.jdbc.resultset=INFO,jdbc
log4j.additivity.jdbc.resultset=false

! Log only the SQL that is executed.
log4j.logger.jdbc.sqlonly=DEBUG,sql
log4j.additivity.jdbc.sqlonly=false

! Log timing information about the SQL that is executed.
log4j.logger.jdbc.sqltiming=DEBUG,sqltiming
log4j.additivity.jdbc.sqltiming=false

! Log connection open/close events and connection number dump
log4j.logger.jdbc.connection=FATAL,connection
log4j.additivity.jdbc.connection=false


! the appender used for the JDBC API layer call logging above, sql only
log4j.appender.sql=org.apache.log4j.FileAppender
log4j.appender.sql.File=./logs/sql.log
log4j.appender.sql.Append=false
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n

! the appender used for the JDBC API layer call logging above, sql timing
log4j.appender.sqltiming=org.apache.log4j.FileAppender
log4j.appender.sqltiming.File=./logs/sqltiming.log
log4j.appender.sqltiming.Append=false
log4j.appender.sqltiming.layout=org.apache.log4j.PatternLayout
log4j.appender.sqltiming.layout.ConversionPattern=-----> %d{yyyy-MM-dd HH:mm:ss.SSS} %m%n%n

! the appender used for the JDBC API layer call logging above
log4j.appender.jdbc=org.apache.log4j.FileAppender
log4j.appender.jdbc.File=./logs/jdbc.log
log4j.appender.jdbc.Append=false
log4j.appender.jdbc.layout=org.apache.log4j.PatternLayout
log4j.appender.jdbc.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %m%n

! the appender used for the JDBC Connection open and close events
log4j.appender.connection=org.apache.log4j.FileAppender
log4j.appender.connection.File=./logs/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

 

6.更多细节以及设置,请参照主页

 

 

 

 

参照log4jdbc的文档,在log4jdbc.properties文件中配置参数。\

  • log4jdbc.debug.stack.prefix,只显示含调用栈的包名的语句。
  • log4jdbc.sqltiming.warn.threshold,执行时间阀值,单位为ms,将Log级别调为Warning,则只会打印执行较慢的语句。
  • log4jdbc.dump.sql.select/insert/update/delete, 过滤掉某些类型的语句。

阅读全文……

标签 : , ,



发表评论 发送引用通报