log4j2配置

3/22/2021

废话不多**,直接上代码

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <!-- 定义日志输出的根目录 -->
    <Properties>
        <Property name="log.dir">./logs</Property>
    </Properties>
    <!-- 定义日志输出格式 -->
    <Appenders>
        <!-- 控制台输出 -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}.%M (%F:%L) - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=green, DEBUG=cyan, TRACE=white}"/>
        </Console>
        <!-- 滚动文件输出ERROR -->
        <RollingFile name="ErrorLog" fileName="${log.dir}/error/error.log" filePattern="${log.dir}/error/error-%d{yyyy-MM-dd}.log.gz">
            <!-- 过滤,属于error的接受,不属于的舍弃下同 -->
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}.%M (%F:%L) - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <DefaultRolloverStrategy max="30"/>
        </RollingFile>
        <!-- 滚动文件输出INFO -->
        <RollingFile name="InfoLog" fileName="${log.dir}/info/info.log" filePattern="${log.dir}/info/info-%d{yyyy-MM-dd}.log.gz">
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}.%M (%F:%L) - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <DefaultRolloverStrategy max="30"/>
        </RollingFile>
        <!-- 滚动文件输出ERROR -->
        <RollingFile name="WarnLog" fileName="${log.dir}/warn/warn.log" filePattern="${log.dir}/warn/warn-%d{yyyy-MM-dd}.log.gz">
            <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}.%M (%F:%L) - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <DefaultRolloverStrategy max="30"/>
        </RollingFile>
    </Appenders>
    <!-- 定义日志级别和输出目标 -->
    <Loggers>
        <!-- 根据包名定义日志级别和输出目标 -->
        <!-- <Logger name="com.example" level="INFO" additivity="false">
            <AppenderRef ref="RollingFile"/>
            <AppenderRef ref="Console"/>
        </Logger> -->
        <!-- 定义默认的日志级别和输出目标 -->
        <Root level="info">
            <!-- <AppenderRef ref="ErrorLog"/>
            <AppenderRef ref="InfoLog"/>
            <AppenderRef ref="WarnLog"/> -->
            <AppenderRef ref="Console"/>
        </Root>
        <!-- 定义异步输出配置 -->
    </Loggers>
</Configuration>

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
Last Updated: 10/30/2024, 10:57:00 AM