如果使用spring插件创建一个spring template project,它会默认带log4j,只要改下log4j的配置就可以使用了,如果自己创建的project,就要加载下log4f的包了,使用步骤如下
1.pom.xml加入log4j的依赖包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<!-- Logging --> < dependency >
< groupId >org.slf4j</ groupId >
< artifactId >slf4j-api</ artifactId >
< version >1.6.6</ version >
</ dependency >
< dependency >
< groupId >org.slf4j</ groupId >
< artifactId >jcl-over-slf4j</ artifactId >
< version >1.6.6</ version >
< scope >runtime</ scope >
</ dependency >
< dependency >
< groupId >org.slf4j</ groupId >
< artifactId >slf4j-log4j12</ artifactId >
< version >1.6.6</ version >
< scope >runtime</ scope >
</ dependency >
|
2.在src/main/resources下创建log4j.xml
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"> < log4j:configuration xmlns:log4j = "http://jakarta.apache.org/log4j/" >
<!-- [控制台STDOUT] -->
< appender name = "console" class = "org.apache.log4j.ConsoleAppender" >
< param name = "encoding" value = "GBK" />
< param name = "target" value = "System.out" />
< layout class = "org.apache.log4j.PatternLayout" >
< param name = "ConversionPattern" value = "%-5p %c{2} - %m%n" />
</ layout >
</ appender >
<!-- [公共Appender] -->
< appender name = "DEFAULT-APPENDER" class = "org.apache.log4j.DailyRollingFileAppender" >
< param name = "File" value = "${webapp.root}/logs/common-default.log" />
< param name = "Append" value = "true" />
< param name = "encoding" value = "GBK" />
< param name = "DatePattern" value = "'.'yyyy-MM-dd'.log'" />
< layout class = "org.apache.log4j.PatternLayout" >
< param name = "ConversionPattern" value = "%d %-5p %c{2} - %m%n" />
</ layout >
</ appender >
<!-- [错误日志APPENDER] -->
< appender name = "ERROR-APPENDER" class = "org.apache.log4j.DailyRollingFileAppender" >
< param name = "File" value = "${webapp.root}/logs/common-error.log" />
< param name = "Append" value = "true" />
< param name = "encoding" value = "GBK" />
< param name = "threshold" value = "error" />
< param name = "DatePattern" value = "'.'yyyy-MM-dd'.log'" />
< layout class = "org.apache.log4j.PatternLayout" >
< param name = "ConversionPattern" value = "%d %-5p %c{2} - %m%n" />
</ layout >
</ appender >
<!-- [组件日志APPENDER] -->
< appender name = "COMPONENT-APPENDER"
class = "org.apache.log4j.DailyRollingFileAppender" >
< param name = "File" value = "${webapp.root}/logs/logistics-component.log" />
< param name = "Append" value = "true" />
< param name = "encoding" value = "GBK" />
< param name = "DatePattern" value = "'.'yyyy-MM-dd'.log'" />
< layout class = "org.apache.log4j.PatternLayout" >
< param name = "ConversionPattern" value = "%d %-5p %c{2} - %m%n" />
</ layout >
</ appender >
<!-- [组件日志] -->
< logger name = "LOGISTICS-COMPONENT" >
< level value = "${loggingLevel}" />
< appender-ref ref = "COMPONENT-APPENDER" />
< appender-ref ref = "ERROR-APPENDER" />
</ logger >
<!-- Root Logger -->
< root >
< level value = "${rootLevel}" ></ level >
< appender-ref ref = "DEFAULT-APPENDER" />
< appender-ref ref = "ERROR-APPENDER" />
</ root >
</ log4j:configuration >
|
og4j.xml中定义了4个appender,就是log输出的位置,一个是控制台,三个是文件,但有一个文件common-error.log是专用于收集特殊错误的文件,可以定义多个分别为不同的场景使用,log4j的相关定义如下:
(1). 输出方式appender一般有5种:
org.apache.log4j.RollingFileAppender(滚动文件,自动记录最新日志)
org.apache.log4j.ConsoleAppender (控制台)
org.apache.log4j.FileAppender (文件)
org.apache.log4j.DailyRollingFileAppender (每天产生一个日志文件)
org.apache.log4j.WriterAppender (将日志信息以流格式发送到任意指定的地方)
(2). 日记记录的优先级priority,优先级由高到低分为
OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL。
Log4j建议只使用FATAL ,ERROR ,WARN ,INFO ,DEBUG这五个级别。
(3). 格式说明layout中的参数都以%开始,后面不同的参数代表不同的格式化信息(参数按字母表顺序列出):
%c 输出所属类的全名,可在修改为 %d{Num} ,Num类名输出的维(如:"org.apache.elathen.ClassName",%C{2}将输出elathen.ClassName)
%d 输出日志时间其格式为 %d{yyyy-MM-dd HH:mm:ss,SSS},可指定格式 如 %d{HH:mm:ss}
%l 输出日志事件发生位置,包括类目名、发生线程,在代码中的行数
%n 换行符
%m 输出代码指定信息,如info(“message”),输出message
%p 输出优先级,即 FATAL ,ERROR 等
%r 输出从启动到显示该log信息所耗费的毫秒数
%t 输出产生该日志事件的线程名
3. web.xml中配置log4j
<!-- 配置加载log4j.xml文件路径 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/log4j.xml</param-value>
</context-param>
<!-- 配置log4j.xml变量 -->
<context-param>
<param-name>rootLevel</param-name>
<param-value>DEBUG</param-value>
</context-param>
<context-param>
<param-name>loggingLevel</param-name>
<param-value>INFO</param-value>
</context-param>
<!-- 配置log4j.xml监听器 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
4. 类使用log4j
(1)包引入
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
(2)日志常量定义
/** 组件日志 */
private static final Logger logger = LoggerFactory .getLogger(LoggerNames.LOGISTICS_COMPONENT);
(3)日志打印
logger.info("日志打印");
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
|
< dependency >
< groupId >org.slf4j</ groupId >
< artifactId >jcl-over-slf4j</ artifactId >
< version >1.7.7</ version >
</ dependency >
< dependency >
< groupId >org.slf4j</ groupId >
< artifactId >slf4j-log4j12</ artifactId >
< version >1.7.7</ version >
</ dependency >
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-context</ artifactId >
< version >${spring.version}</ version >
< exclusions >
< exclusion >
< groupId >commons-logging</ groupId >
< artifactId >commons-logging</ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
|
2.log4j配置
1
2
3
4
5
6
7
|
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n log4j.logger.org.springframework.beans.factory=DEBUG |
3.代码用法:
1
2
3
4
5
6
7
8
9
|
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UserService {
final Logger logger = LoggerFactory.getLogger(UserService. class );
public boolean save(){
logger.error( "虽然{}喜欢我,但是我喜欢{}" , "A" , "B" );
return true ;
}
}
|