标签:服务器 启动 set 示例 common oss lin value error
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记录日志使用方法
标签:服务器 启动 set 示例 common oss lin value error
原文地址:https://www.cnblogs.com/hnhycnlc888/p/9213752.html