spring本身使用的commons-logging来做日志。为了集成logback需要先去掉commons-logging之后再加入jcl-over-slf4j的依赖作桥接,最后加入logback-classic的依赖来做日志处理。
首先每个spring的依赖中排除掉commons-logging依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

再加入logback桥接和实现

1
2
3
4
5
6
7
8
9
10
11
<!-- logback -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>

接下来添加logback.xml到resources目录下,配置大致如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>

<appender name="LOGFILE"
class="ch.qos.logback.core.rolling.RollingFileAppender" >
<Encoding>UTF-8</Encoding>
<encoder>
<pattern>%d %p [%t] [%c]:%L - %m%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>d:/tmp/%d{yyyy-MM-dd-HH'.log'}</fileNamePattern>
</rollingPolicy>
</appender>

<logger name="org.springframework" level="INFO" />
<logger name="java.sql.PreparedStatement" level="INFO" />
<logger name="ch.qos.logback" level="WARN" />
<logger name="com.chinasofti.framework.ssm" level="DEBUG" />

<root level="INFO">
<appender-ref ref="LOGFILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>

参考
spring手册相关内容
logback手册配置