升级过程中遇到些许波折,如无法输出 ibatis jdbc 日志了,记下随笔供参阅。
首先要知道项目中原有日志情况,本项目中使用了spring 4, ibatis 2.3.x,
spring 使用了JCL, ibatis动态加载JCL、Log4j、jdk14log
先删除原log4j 和 JCL dependency,然而ibatis不支持sl4j,而是首先查找JCL,所以考虑使用 jcl-over-slf4j 桥接到 slf4j 。
maven依赖
<!-- 加入logback 、 slf4j依赖包 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.6</version>
</dependency>
加了jcl-over-slf4j 却未能生效,是因为有开源包依赖了jcl,用mvn命令找出这些包,然后exclusion掉
mvn dependency:tree
[INFO] | | \- dom4j:dom4j:jar:1.6.1:compile
[INFO] | | \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] | +- com.whalin:Memcached-Java-Client:jar:3.0.0:compile
[INFO] | \- com.baidu:bccs-api-lib:jar:1.1.2:compile
[INFO] +- org.springframework:spring-test:jar:4.0.0.RELEASE:test
[INFO] | \- org.springframework:spring-core:jar:4.0.0.RELEASE:compile
[INFO] | \- commons-logging:commons-logging:jar:1.1.1:compile //找到这个jar,定位是哪个开源jar依赖的
[INFO] +- junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- commons-dbcp:commons-dbcp:jar:20030825.184428:test
[INFO] +- org.apache.commons:commons-lang3:jar:3.3.2:compile
[INFO] +- org.apache.ibatis:ibatis-sqlmap:jar:2.3.4.726:compile
[INFO] \- org.springframework:spring-orm:jar:3.2.6.RELEASE:compile
[INFO] +- aopalliance:aopalliance:jar:1.0:compile
exclusion掉JCL,然后重复以上命令,直到再无
commons-logging 出现
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
使用SLF4J Migrator升级历史代码
运行slf4j-migrator-XXX.jar文件,可将历史代码中的Jakarta Commons Logging (JCL) API、 log4j、java.util.logging(JUL )替换成 slf4j代码。实际上就是加入import和修改日志成员变量声明并把原日志代码删除。
java -jarslf4j-migrator-1.7.10.jar
日志文件有两种,一种是存放所有级别(依项目中配置)日志;一种只存异常日志;
日志文件命名方式:每天一个日志文件logistic-admin-err-2014-09-09.1.log,
在同一天内如果超出50M,序号加1如:
logistic-admin-err-2014-09-09.2.log
logistic-admin-err-2014-09-09.3.log ...
logback xml:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="3 seconds">
<property name="LOG_DIR" value="../logs" />
<property name="LOG_LEVEL_JDBC" value="DEBUG" />
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{45} - %msg%n
</pattern>
</layout>
</appender>
<!--日志文件:每天一个日志文件logistic-admin-err-%d{yyyy-MM-dd}.1.log,
如果超出50M,序号加1如:logistic-admin-err-%d{yyyy-MM-dd}.2.log logistic-admin-err-%d{yyyy-MM-dd}.3.log ...-->
<appender name="FILEALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/logistic-admin.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/logistic-admin-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{45} - %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
<!-- deny all events with a level below INFO, that is TRACE and DEBUG -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
</appender>
<!--错误日志文件-->
<appender name="FILEERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/logistic-admin-err.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/logistic-admin-err-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>30MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{45} - %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!--本项目日志级别-->
<logger name="com.incito.logistics" level="debug"/>
<!--依赖框架logging-->
<logger name="org.springframework" level="info"/>
<!--jdbc logging-->
<logger name="com.ibatis.common.jdbc.SimpleDataSource" level="${LOG_LEVEL_JDBC}"/>
<logger name="com.ibatis.common.jdbc.ScriptRunner" level="${LOG_LEVEL_JDBC}"/>
<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="${LOG_LEVEL_JDBC}"/>
<logger name="java.sql.Connection" level="${LOG_LEVEL_JDBC}"/>
<logger name="java.sql.Statement" level="${LOG_LEVEL_JDBC}"/>
<logger name="java.sql.ResultSet" level="${LOG_LEVEL_JDBC}"/>
<logger name="java.sql.PreparedStatement" level="${LOG_LEVEL_JDBC}"/>
<root>
<level level="info" value="on"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILEALL"/>
<appender-ref ref="FILEERROR"/>
</root>
</configuration>