log4j2 Best Practices

原理我就不讲述了,我就简单的说说我的配置以及知识要点

  1. 过滤日志级别,对应的级别存档相应的文件。如info只存info级别的信息,比其高的级别直接过滤。
  2. 单个log文件体积控制在100mb,超过则自动增加新的文件。日志文件名序号自增起始位以1开始,当日总数超过100自动替换较早的文件.
  3. 日志压缩存档,按月归档,
  4. 自定义日志级别
  5. 日志持久化,将日志信息存储到mongodb

pom.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
<log4j2.version>2.8.1</log4j2.version>

<!-- log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-nosql</artifactId>
<version>${log4j2.version}</version>
</dependency>

log4j2.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn">

<properties>
<!-- #(ALL<)TRACE<DEBUG<INFO<WARN<ERROR<FATAL(<OFF) -->
<property name="LEVEL">debug</property>
<property name="PROJECT_NAME">log4j2</property>
<property name="LOG_PATH">/Users/fred/Desktop/logs/${PROJECT_NAME}</property>
</properties>

<CustomLevels>
<CustomLevel name="DIAG" intLevel="3" />
<CustomLevel name="NOTICE" intLevel="4" />
<CustomLevel name="VERBOSE" intLevel="5" />
</CustomLevels>

<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<ThresholdFilter level="${LEVEL}" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout
pattern="[${PROJECT_NAME}][%-d{yyyy-MM-dd HH:mm:ss:sss}][%p] %C.%M(%L) | %m%n" />
</Console>

<RollingFile name="INFO_ROLLING_FILE" fileName="${LOG_PATH}/${PROJECT_NAME}_info.log"
filePattern="${LOG_PATH}/$${date:yyyy-MM}/${PROJECT_NAME}_info_%d{yyyy-MM-dd}-%i.log.gz">
<Filters>
<!--
此Filter意思是,只输出info级别的数据
DENY,日志将立即被抛弃不再经过其他过滤器;
NEUTRAL,有序列表里的下个过滤器过接着处理日志;
ACCEPT,日志会被立即处理,不再经过剩余过滤器。
-->
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
</Filters>
<PatternLayout
pattern="[${PROJECT_NAME}][%-d{yyyy-MM-dd HH:mm:ss:sss}][%p] %C.%M(%L) | %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="99 MB"/>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="100" />
</RollingFile>

<RollingFile name="DEBUG_ROLLING_FILE" fileName="${LOG_PATH}/${PROJECT_NAME}_debug.log"
filePattern="${LOG_PATH}/$${date:yyyy-MM}/${PROJECT_NAME}_debug_%d{yyyy-MM-dd}-%i.log.gz">
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL" />
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" />
</Filters>
<PatternLayout
pattern="[${PROJECT_NAME}][%-d{yyyy-MM-dd HH:mm:ss:sss}][%p] %C.%M(%L) | %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="99 MB"/>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="100" />
</RollingFile>

<RollingFile name="ERROR_ROLLING_FILE" fileName="${LOG_PATH}/${PROJECT_NAME}_error.log"
filePattern="${LOG_PATH}/$${date:yyyy-MM}/${PROJECT_NAME}_error_%d{yyyy-MM-dd}-%i.log.gz">
<ThresholdFilter level="error" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout
pattern="[${PROJECT_NAME}][%-d{yyyy-MM-dd HH:mm:ss:sss}][%p] %C.%M(%L) | %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="99 MB"/>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="100" />
</RollingFile>

<!-- 持久化日志到mongodb -->
<NoSql name="mongoAppender" bufferSize="10"> <!--建议bufferSize配置,减少频繁写操作。-->
<MongoDb databaseName="log4j2" collectionName="${PROJECT_NAME}_logs" server="127.0.0.1" port="27017"/>
<!--此处需要一个能获取MongoDBClient或者DB的类,并指定获取的方法名
<MongoDb databaseName="log4j2" collectionName="${PROJECT_NAME}_logs"
factoryClassName="com.fanfq.plugin.log4j2.MongoLog4jAppender"
factoryMethodName="getMongoClient"/>
-->
</NoSql>

<!-- 自定义日志级别 -->
<RollingFile name="DIAG_ROLLING_FILE" fileName="${LOG_PATH}/${PROJECT_NAME}_diag.log"
filePattern="${LOG_PATH}/$${date:yyyy-MM}/${PROJECT_NAME}_diag_%d{yyyy-MM-dd}-%i.log.gz">
<ThresholdFilter level="DIAG" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout
pattern="[${PROJECT_NAME}][%-d{yyyy-MM-dd HH:mm:ss:sss}][%p] %C.%M(%L) | %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="99 MB"/>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="100" />
</RollingFile>

<RollingFile name="NOTICE_ROLLING_FILE" fileName="${LOG_PATH}/${PROJECT_NAME}_notice.log"
filePattern="${LOG_PATH}/$${date:yyyy-MM}/${PROJECT_NAME}_notice_%d{yyyy-MM-dd}-%i.log.gz">
<ThresholdFilter level="NOTICE" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout
pattern="[${PROJECT_NAME}][%-d{yyyy-MM-dd HH:mm:ss:sss}][%p] %C.%M(%L) | %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="99 MB"/>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="100" />
</RollingFile>

</appenders>

<loggers>
<!-- log持久化 -->
<Logger name="mongologger" level="info">
<appender-ref ref="mongoAppender"/>
</Logger>

<root level="${LEVEL}">
<appender-ref ref="CONSOLE" />
<appender-ref ref="INFO_ROLLING_FILE" />
<appender-ref ref="DEBUG_ROLLING_FILE"/>
<appender-ref ref="ERROR_ROLLING_FILE" />
</root>
</loggers>
</configuration>

Log4j2Test.java

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
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

public class Log4j2Test {

public static final Logger logger = LogManager.getLogger(Testing.class);
public static final Logger mongologger = LogManager.getLogger("mongologger");

@Test
public void logger() {
logger.debug("debug1122999000");
logger.info("info");
logger.warn("warn");
logger.error("error");
}

@Test
public void mongologger() {
//持久化日志到mongodb
mongologger.debug("mongologger-debug1122999000");
mongologger.info("mongologger-info");
mongologger.warn("mongologger-warn");
mongologger.error("mongologger-error");
}

@Test
public void customlogger() {
//自定义日志级别
Level diag = Level.getLevel("DIAG");
Level notice = Level.getLevel("NOTICE");

logger.log(diag,"a diagnostic message");
logger.log(notice,"a notice message");
}

}
Fred范方青 wechat
项目合作请联系我私人微信: fredtv23
0%