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
<?xml version="1.0" encoding="UTF-8" ?>
<configuration status="warn">

<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%p] %d %c %l - %m%n"/>
</Console>

<RollingFile name="activity" fileName="/opt/fox.log"
filePattern="/opt/fox.log.%d{yyyy-MM-dd}.gz">
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<PatternLayout pattern="[%p] %d - %m%n" charset="UTF-8"/>
</RollingFile>
<RollingFile name="fox_err" fileName="/opt/fox_err.log"
filePattern="/opt/fox_err..log.%d{yyyy-MM-dd}.gz">
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<PatternLayout pattern="[%p] %d %l - %m%n" charset="UTF-8"/>
</RollingFile>
</Appenders>
<Loggers>
<logger name="com.fox" additivity="false" level="info">
<appender-ref ref="activity" />
<appender-ref ref="activity_err" level="error"/>
</logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>

</configuration>

Compared to Log4j, there are several major changes:

First, the overall structure has changed significantly — appenders and loggers are now each organized under their own root nodes.

The XML node names have also been redesigned to use descriptive names directly, instead of the previous appender xxx="xxx" and param xxx="xxx" format.

Additionally, certain attributes such as fileName can only be configured as node attributes, not as child nodes as was possible in Log4j.

Furthermore, Log4j2 supports compression when archiving. By specifying a compressed file extension such as .gz or .zip in the filePattern attribute of the RollingFile node, Log4j2 will automatically select the appropriate compression algorithm.

That covers the main differences I’ve discovered so far. By importing this XML configuration along with the log4j-core and log4j-api packages, you can start using Log4j2. Additionally, if needed, you can use log4j-slf4j-impl, log4j-jcl, and log4j-1.2-api to achieve compatibility with SLF4J, JCL, and Log4j respectively.


Source: https://lichuanyang.top/en/posts/41673/