log4j 使用指南
依赖
版本: 2.10.0
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
<!-- log4j 核心jar包 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<!-- log4j 核心jar包 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
<!-- 将 common-logging 的日志桥接到 log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.10.0</version>
</dependency>
<!-- 将 log4j 1.x 的日志桥接到 log4j 2.x -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.10.0</version>
</dependency>
<!-- 将 java.util.logging 的日志桥接到 log4j 2.x -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<version>2.10.0</version>
</dependency>
<!-- 将 slf4j 的日志桥接到 log4j 2.x。不要同时引用 log4j-to-slf4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.10.0</version>
</dependency>
<!-- 将 log4j 2.x 的日志桥接到 slf4j。不要同时引用 log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.10.0</version>
</dependency>
<!-- 在servlet或jsp容器中强烈建议引入该依赖,以解决log4j启动和卸载时的资源清理问题 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.10.0</version>
</dependency>
配置
基本配置
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<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<!-- 自定义属性 -->
<!-- 设置 log 目录 -->
<Property name="log.dir">D:/logs/demo</Property>
<!-- 设置日志文件编码 -->
<Property name="charset.">D:/logs/demo</Property>
</Properties>
<Appenders>
<!-- 控制台appender -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout charset="${log.charset}" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger - %msg %ex %n"/>
</Console>
<RollingFile name="appFileLog" fileName="${log.dir}/app.log"
filePattern="${log.dir}/%d{yyyy-MM-dd}/app.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger - %msg %ex %n"/>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- 默认日志级别 debug -->
<Root level="debug">
<!-- 默认日志输出到 控制台 -->
<AppenderRef ref="Console"/>
<AppenderRef ref="appFileLog" />
</Root>
<!-- 打印spring info日志 -->
<logger name="org.springframework" level="info" />
</Loggers>
</Configuration>
web环境配置
指定log4j日志配置文件
1
2
3
4<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
servlet 3.0
在servlet 3.0 环境中,log4j2 可以自动注册启动和卸载监听器。如果不需要自动注册,可以通过以下配置禁用。
1
2
3
4<context-param>
<param-name>isLog4jAutoInitializationDisabled</param-name>
<param-value>true</param-value>
</context-param>
servlet 2.5
在servlet 2.5 环境中,必须手动指定 Log4jServletContextListener
以注册 log4j 启动和卸载监听器
1
2
3<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
使用
1
2
3
4
5
6
7
8
9
10
11
12import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class HelloWorld {
private static final Logger logger = LogManager.getLogger("HelloWorld");
public static void main(String[] args) {
logger.info("Hello, World!");
if (logger.isDebugEnabled()) {
logger.debug("args[0]={}", args[0]);
}
}
}