Log4j2使用和配置
发表于:2025-01-22 | 分类: 编程语言 Java

Maven依赖

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
<!-- log配置:Log4j2 + Slf4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.2</version>
</dependency>
<dependency> <!-- 桥接:告诉Slf4j使用Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.2</version>
</dependency>
<dependency> <!-- 桥接:告诉commons logging使用Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>

配置文件

log4j2.properties

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
property.logLevel = INFO
property.logDir = ./logs/
property.logLayout = [%d{yyyy-MM-dd HH:mm:ss.SSS}][%-5level] %c{1} [%t] - %msg%n

rootLogger.level = ${logLevel}
rootLogger.appenderRef.stdout.ref = console
rootLogger.appenderRef.file.ref = rolling_file

appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = ${logLayout}

appender.rollingFile.type = RollingFile
appender.rollingFile.name = rolling_file
appender.rollingFile.fileName=${logDir}/vpc_zk_cos_backup_v2.log
appender.rollingFile.filePattern = ${logDir}/vpc_zk_cos_backup_v2-%d{MM-dd-yy-HH-mm-ss}-%i.log
appender.rollingFile.layout.type = PatternLayout
appender.rollingFile.layout.pattern = ${logLayout}
appender.rollingFile.policies.type = Policies
appender.rollingFile.policies.size.type = SizeBasedTriggeringPolicy
appender.rollingFile.policies.size.size=100MB
appender.rollingFile.strategy.type = DefaultRolloverStrategy
appender.rollingFile.strategy.max = 5

上一篇:
taskflow介绍
下一篇:
Jupyter安装和配置