Log4j記錄日誌使用方法
阿新 • • 發佈:2018-06-22
img doc 導入 add cte ctype In url nic
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
</layout>
</appender>
<!--
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
-->
<!-- <param name="file" value="${user.home}/foss-framework.log" /> -->
<!-- <param name="append" value="true" /> -->
<!-- <param name="maxFileSize" value="10MB" /> -->
<!-- <param name="maxBackupIndex" value="100" /> -->
<!-- <layout class="org.apache.log4j.PatternLayout"> -->
<!--
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) -
%m%n" />
-->
<!-- </layout> -->
<!-- </appender> -->
<!-- <appender name="framework" -->
<!--
class="com.deppon.foss.framework.server.components.logger.BufferedAppender">
-->
<!-- <layout class="org.apache.log4j.PatternLayout"> -->
<!--
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}]
%5p %c{2}: %m%n" />
-->
<!-- </layout> -->
<!-- </appender> -->
<!-- 下面是打印 mybatis語句的配置 -->
<logger name="com.ibatis" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Connection" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Statement" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.PreparedStatement" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.ResultSet" additivity="true">
<level value="DEBUG" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="CONSOLE" />
<!-- <appender-ref ref="FILE" /> -->
<!-- <appender-ref ref="framework" /> -->
</root>
</log4j:configuration>
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.junit.Test;
6
7
8
9 public class logTest {
10
11 private Log log=LogFactory.getLog(this.getClass());
12
13
14 @Test
15 public void test() throws Exception {
16 log.debug(" this is a debug from jyh‘s test");
17 log.info("this is a info from jyh‘s test");
18 log.warn("this is a warn from jyh‘s test");
19 log.error("this is a error from jyh‘s test");
20 log.fatal("this is a fatal from jyh‘s test");
21 }
22 }
1.導入相關JAR包
log4j-1.2.15.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.1.jar log4jdbc4-1.2.jar
2.配置log4j.xml,具體配置根據項目自己定義
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" />
</layout>
</appender>
<!--
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
-->
<!-- <param name="file" value="${user.home}/foss-framework.log" /> -->
<!-- <param name="append" value="true" /> -->
<!-- <param name="maxFileSize" value="10MB" /> -->
<!-- <param name="maxBackupIndex" value="100" /> -->
<!-- <layout class="org.apache.log4j.PatternLayout"> -->
<!--
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) -
%m%n" />
-->
<!-- </layout> -->
<!-- </appender> -->
<!-- <appender name="framework" -->
<!--
class="com.deppon.foss.framework.server.components.logger.BufferedAppender">
-->
<!-- <layout class="org.apache.log4j.PatternLayout"> -->
<!--
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}]
%5p %c{2}: %m%n" />
-->
<!-- </layout> -->
<!-- </appender> -->
<!-- 下面是打印 mybatis語句的配置 -->
<logger name="com.ibatis" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Connection" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.Statement" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.PreparedStatement" additivity="true">
<level value="DEBUG" />
</logger>
<logger name="java.sql.ResultSet" additivity="true">
<level value="DEBUG" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="CONSOLE" />
<!-- <appender-ref ref="FILE" /> -->
<!-- <appender-ref ref="framework" /> -->
</root>
</log4j:configuration>
3.修改連接驅動和連接url
比如: jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8 改為: jdbc.driverClass=net.sf.log4jdbc.DriverSpy jdbc.jdbcUrl=jdbc:log4jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
完成以上三步,重新啟動服務器就OK了,你就可以看到sql全部語句了。
程序日誌記錄示例:
1 package cn.oa2014.oa.test;2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.junit.Test;
6
7
8
9 public class logTest {
10
11 private Log log=LogFactory.getLog(this.getClass());
12
13
14 @Test
15 public void test() throws Exception {
16 log.debug(" this is a debug from jyh‘s test");
17 log.info("this is a info from jyh‘s test");
18 log.warn("this is a warn from jyh‘s test");
19 log.error("this is a error from jyh‘s test");
20 log.fatal("this is a fatal from jyh‘s test");
21 }
22 }
日誌記錄如:
Log4j記錄日誌使用方法