spring默认使用了commons-logging,在集成其它日志框架,slf4j使用了编译时绑定来代替运行时发现.它注定是更高效的(当然slf4j还可以使用占位符绑定等功能).下文介绍spring三种常用方案记录日志,推荐第三种
方案一:commons-logging+Log4J
1.依赖:
1
2
3
4
5
6
7
8
9
10
11 1<dependency>
2 <groupId>org.springframework</groupId>
3 <artifactId>spring-context</artifactId>
4 <version>${spring.version}</version>
5</dependency>
6<dependency>
7 <groupId>log4j</groupId>
8 <artifactId>log4j</artifactId>
9 <version>1.2.17</version>
10</dependency>
11
2.在classpath根目录添加log4j.properties或log4j.xml,关于开发的日志级别配置,调得太高,会漏到一些信息看不见,调得太低,输出得太多,看得眼花缭乱,在产品阶段甚至影响应用性能.我觉得开发时,默认(一般依赖也是用默认appender)给WARN,项目本身编写的代码可以给DEBUG,其它另外特别指定,以下是log4j.properties的一个例子:
1
2
3
4
5
6
7
8
9 1log4j.rootCategory=WARN, stdout
2
3log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
6
7log4j.category.org.exam=DEBUG
8log4j.category.org.springframework.beans.factory=INFO
9
1
2
3
4
5
6
7
8
9 1log4j.rootLogger=WARN, stdout
2
3log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
6
7log4j.logger.org.exam=DEBUG
8log4j.logger.org.springframework.beans.factory=INFO
9
3.代码用法:
1
2
3
4
5
6
7
8
9 1import org.apache.log4j.Logger;
2public class UserService {
3 final Logger logger = Logger.getLogger(UserService.class);
4 public boolean save(){
5 logger.error("hello");
6 return true;
7 }
8}
9
方案二:slf4j+Log4J
1.先在spring排除commons-logging.再引入4个包:桥接包jcl-over-slf4j,SLF4J API包slf4j-api,绑定到Log4J的包slf4j-log4j12,Log4J实现包log4j12.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 1<dependency>
2 <groupId>org.slf4j</groupId>
3 <artifactId>jcl-over-slf4j</artifactId>
4 <version>1.7.7</version>
5</dependency>
6<dependency>
7 <groupId>org.slf4j</groupId>
8 <artifactId>slf4j-log4j12</artifactId>
9 <version>1.7.7</version>
10</dependency>
11<dependency>
12 <groupId>org.springframework</groupId>
13 <artifactId>spring-context</artifactId>
14 <version>${spring.version}</version>
15 <exclusions>
16 <exclusion>
17 <groupId>commons-logging</groupId>
18 <artifactId>commons-logging</artifactId>
19 </exclusion>
20 </exclusions>
21</dependency>
22
2.log4j配置同上.
3.代码用法:
1
2
3
4
5
6
7
8
9
10 1import org.slf4j.Logger;
2import org.slf4j.LoggerFactory;
3public class UserService {
4 final Logger logger = LoggerFactory.getLogger(UserService.class);
5 public boolean save(){
6 logger.error("hello {}","A");
7 return true;
8 }
9}
10
方案三:slf4j+logback.其中,logback是slf4j原生的实现.
1.先引入logback-classic依赖,它会将其它依赖引入,再在spring排除commons-logging.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1<dependency>
2 <groupId>ch.qos.logback</groupId>
3 <artifactId>logback-classic</artifactId>
4 <version>1.1.3</version>
5</dependency>
6<dependency>
7 <groupId>org.springframework</groupId>
8 <artifactId>spring-webmvc</artifactId>
9 <version>${spring.version}</version>
10 <exclusions>
11 <exclusion>
12 <groupId>commons-logging</groupId>
13 <artifactId>commons-logging</artifactId>
14 </exclusion>
15 </exclusions>
16</dependency>
17
2.logback配置:查找顺序logback.groovy–>logback-test.xml–>logback.xml,详细配置还是参考文档http://logback.qos.ch/documentation.html.下面是logback.xml例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1<?xml version="1.0" encoding="UTF-8"?>
2<configuration>
3 <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
4 <encoder>
5 <pattern>%d{yy.MM.dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
6 </encoder>
7 </appender>
8 <logger name="org.exam" level="debug" />
9 <logger name="org.springframework.beans.factory" level="info" />
10 <root level="warn">
11 <appender-ref ref="console"/>
12 </root>
13</configuration>
14
3.代码用法:同方案二一样