Skip to content

Commit 123a087

Browse files
committed
🔖 log4j2 示例
1 parent 6898a13 commit 123a087

3 files changed

Lines changed: 229 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
7+
<!-- [Part 1] BASIC SETTINGS BEGIN -->
8+
9+
<!-- MAVEN COORDINATE BEGIN -->
10+
<groupId>io.github.dunwu</groupId>
11+
<artifactId>javalib-log-log4j2</artifactId>
12+
<version>1.0.0</version>
13+
<packaging>jar</packaging>
14+
<!-- MAVEN COORDINATE END -->
15+
16+
<!-- RELATIONSHIP SETTINGS BEGIN -->
17+
<dependencies>
18+
<!-- log start -->
19+
<dependency>
20+
<groupId>org.slf4j</groupId>
21+
<artifactId>slf4j-api</artifactId>
22+
<version>1.7.25</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.apache.logging.log4j</groupId>
26+
<artifactId>log4j-slf4j-impl</artifactId>
27+
<version>2.11.0</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.apache.logging.log4j</groupId>
31+
<artifactId>log4j-api</artifactId>
32+
<version>2.11.0</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.apache.logging.log4j</groupId>
36+
<artifactId>log4j-core</artifactId>
37+
<version>2.11.0</version>
38+
</dependency>
39+
<!-- log end -->
40+
41+
<dependency>
42+
<groupId>junit</groupId>
43+
<artifactId>junit</artifactId>
44+
<version>4.12</version>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
<!-- RELATIONSHIP SETTINGS END -->
49+
50+
<!-- PROPERTIES BEGIN -->
51+
<properties>
52+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
53+
<java.version>1.8</java.version>
54+
<maven.compiler.source>${java.version}</maven.compiler.source>
55+
<maven.compiler.target>${java.version}</maven.compiler.target>
56+
</properties>
57+
<!-- PROPERTIES END -->
58+
59+
<!-- [Part 1] BASIC SETTINGS END -->
60+
61+
62+
<!-- [Part 2] BUILD SETTINGS BEGIN -->
63+
<build>
64+
<resources>
65+
<resource>
66+
<filtering>true</filtering>
67+
<directory>src/main/resources</directory>
68+
</resource>
69+
</resources>
70+
</build>
71+
<!-- [Part 2] BUILD SETTINGS END -->
72+
73+
74+
<!-- [Part 3] PROJECT INFO BEGIN -->
75+
<name>${project.artifactId}</name>
76+
<description>log4j2 示例</description>
77+
<!-- [Part 3] PROJECT INFO END -->
78+
79+
80+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.dunwu.javalib.log;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
/**
7+
* Logback 示例
8+
* @author Zhang Peng
9+
* @date 2018/3/29
10+
* @see <a href="https://logback.qos.ch/">logback 官网</a>
11+
*/
12+
public class Log4j2Demo {
13+
14+
private static final Logger logger = LoggerFactory.getLogger(Log4j2Demo.class);
15+
16+
public static void main(String[] args) {
17+
for (int i = 0; i < 10; i++) {
18+
logger.trace("NO.{} 这是一条 {} 日志记录", i, "trace");
19+
logger.debug("NO.{} 这是一条 {} 日志记录", i, "debug");
20+
logger.info("NO.{} 这是一条 {} 日志记录", i, "info");
21+
logger.warn("NO.{} 这是一条 {} 日志记录", i, "warn");
22+
logger.error("NO.{} 这是一条 {} 日志记录", i, "error");
23+
}
24+
}
25+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<properties>
4+
<property name="fileName" value="javalib-log4j2"/>
5+
<property name="logPath" value="D:\home\log"/>
6+
7+
<property name="logPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [javalib] [%t] [%-5level] %c{36}#%M - %msg%n"/>
8+
9+
10+
</properties>
11+
<Appenders>
12+
<Console name="Console" target="SYSTEM_OUT">
13+
<PatternLayout pattern="${logPattern}"/>
14+
</Console>
15+
<RollingFile name="Trace" fileName="${logPath}/${fileName}-trace.log"
16+
filePattern="${logPath}/$${date:yyyy-MM}/${fileName}-trace-%d{yyyy-MM-dd}-%i.log.gz">
17+
<PatternLayout>
18+
<Pattern>${logPattern}</Pattern>
19+
</PatternLayout>
20+
<Filters>
21+
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
22+
</Filters>
23+
<Policies>
24+
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
25+
<SizeBasedTriggeringPolicy size="250 MB"/>
26+
</Policies>
27+
<DefaultRolloverStrategy>
28+
<Delete basePath="${logPath}" maxDepth="2">
29+
<IfFileName glob="*/${fileName}-trace-*.log.gz" />
30+
<IfLastModified age="30d" />
31+
</Delete>
32+
</DefaultRolloverStrategy>
33+
</RollingFile>
34+
<RollingFile name="Debug" fileName="${logPath}/${fileName}-debug.log"
35+
filePattern="${logPath}/$${date:yyyy-MM}/${fileName}-debug-%d{yyyy-MM-dd}-%i.log.gz">
36+
<PatternLayout>
37+
<Pattern>${logPattern}</Pattern>
38+
</PatternLayout>
39+
<Filters>
40+
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
41+
</Filters>
42+
<Policies>
43+
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
44+
<SizeBasedTriggeringPolicy size="250 MB"/>
45+
</Policies>
46+
<DefaultRolloverStrategy>
47+
<Delete basePath="${logPath}" maxDepth="2">
48+
<IfFileName glob="*/${fileName}-debug-*.log.gz" />
49+
<IfLastModified age="30d" />
50+
</Delete>
51+
</DefaultRolloverStrategy>
52+
</RollingFile>
53+
<RollingFile name="Info" fileName="${logPath}/${fileName}-info.log"
54+
filePattern="${logPath}/$${date:yyyy-MM}/${fileName}-info-%d{yyyy-MM-dd}-%i.log.gz">
55+
<PatternLayout>
56+
<Pattern>${logPattern}</Pattern>
57+
</PatternLayout>
58+
<Filters>
59+
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
60+
</Filters>
61+
<Policies>
62+
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
63+
<SizeBasedTriggeringPolicy size="250 MB"/>
64+
</Policies>
65+
<DefaultRolloverStrategy>
66+
<Delete basePath="${logPath}" maxDepth="2">
67+
<IfFileName glob="*/${fileName}-info-*.log.gz" />
68+
<IfLastModified age="30d" />
69+
</Delete>
70+
</DefaultRolloverStrategy>
71+
</RollingFile>
72+
<RollingFile name="Warn" fileName="${logPath}/${fileName}-warn.log"
73+
filePattern="${logPath}/$${date:yyyy-MM}/${fileName}-warn-%d{yyyy-MM-dd}-%i.log.gz">
74+
<PatternLayout>
75+
<Pattern>${logPattern}</Pattern>
76+
</PatternLayout>
77+
<Filters>
78+
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
79+
</Filters>
80+
<Policies>
81+
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
82+
<SizeBasedTriggeringPolicy size="250 MB"/>
83+
</Policies>
84+
<DefaultRolloverStrategy>
85+
<Delete basePath="${logPath}" maxDepth="2">
86+
<IfFileName glob="*/${fileName}-warn-*.log.gz" />
87+
<IfLastModified age="30d" />
88+
</Delete>
89+
</DefaultRolloverStrategy>
90+
</RollingFile>
91+
<RollingFile name="Error" fileName="${logPath}/${fileName}-error.log"
92+
filePattern="${logPath}/$${date:yyyy-MM}/${fileName}-error-%d{yyyy-MM-dd}-%i.log.gz">
93+
<PatternLayout>
94+
<Pattern>${logPattern}</Pattern>
95+
</PatternLayout>
96+
<Filters>
97+
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
98+
</Filters>
99+
<Policies>
100+
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
101+
<SizeBasedTriggeringPolicy size="250 MB"/>
102+
</Policies>
103+
<DefaultRolloverStrategy>
104+
<Delete basePath="${logPath}" maxDepth="2">
105+
<IfFileName glob="*/${fileName}-error-*.log.gz" />
106+
<IfLastModified age="30d" />
107+
</Delete>
108+
</DefaultRolloverStrategy>
109+
</RollingFile>
110+
</Appenders>
111+
112+
<Loggers>
113+
<Logger name="io.github.dunwu.javalib.log" level="trace" additivity="false">
114+
<AppenderRef ref="Trace"/>
115+
<AppenderRef ref="Debug"/>
116+
<AppenderRef ref="Info"/>
117+
<AppenderRef ref="Warn"/>
118+
<AppenderRef ref="Error"/>
119+
</Logger>
120+
<Root level="trace">
121+
<AppenderRef ref="Console"/>
122+
</Root>
123+
</Loggers>
124+
</Configuration>

0 commit comments

Comments
 (0)