Apache log4j初体验

  |   0 评论   |   0 浏览

背景

虽然log4j已经用了好久,但是在写一个新项目时,还是会遇到日志打不出来,日志配置文件不生效等问题,每次排查又比较耗时。

所以本文统一记录一下。

现状

Apache Log4j 2是Log4j的升级版本,有很多重大改进。同时,Log4j2也是Logback的升级版本,修复了一些Logback架构设计的问题。所以Log4j和Logback推荐都升级到Log4j 2。

环境:Log4j 2.13.0+版本需要Java 8;2.14 - 2.12.1版需要Java 7。

初体验

log4j

测试代码

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MainLogger {
    public static void main(String[] args) {
        log.info("test info log");
        log.warn("test warn log");
        log.error("test error log");
    }
}

如果pom中不增加任何依赖,运行时会有提示。

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

所以需要增加下列之一,且只能有一个:slf4j-nop.jar slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar

日志风格

simple log风格

如果pom中加入simple

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>

则日志风格为simple log风格,如下:

[main] INFO example.log.MainLogger - test info log
[main] WARN example.log.MainLogger - test warn log
[main] ERRORexample.log.MainLogger - test error log

jdk14风格

pom为

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.5.6</version>
        </dependency>

日志为

SLF4J: The requested version 1.5.6 by your slf4j binding is not compatible with [1.6, 1.7]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
十月 30, 2021 5:41:39 下午 example.log.MainLogger main
信息: test info log
十月 30, 2021 5:41:39 下午 example.log.MainLogger main
警告: test warn log
十月 30, 2021 5:41:39 下午 example.log.MainLogger main
严重: test error log

log4j2风格

pom文件

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.14.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.14.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.14.1</version>
    </dependency>

结果

21:24:57.832 [main] ERROR example.log.MainLogger - test error log

配置文件 log4j2.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
    </Appenders>
    <Loggers>
        <Root level="warn">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

结果

2021-10-30 21:33:52.329 [main] WARN  example.log.MainLogger - test warn log
2021-10-30 21:33:52.329 [main] ERROR example.log.MainLogger - test error log

Rolling配置

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <RollingFile name="WarningFile" fileName="log/warn.log" filePattern="log/logs/warn.log.%i">
            <LevelRangeFilter minLevel="WARN" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="250MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        <RollingFile name="ErrorFile" fileName="log/error.log" filePattern="log/logs/error.log.%i">
            <LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="250MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <!--        <AsyncLogger name="" level="warn">-->
        <!--            <AppenderRef ref="WarningFile"/>-->
        <!--        </AsyncLogger>-->

        <!--        <AsyncLogger level="error">-->
        <!--            <AppenderRef ref="ErrorFile"/>-->
        <!--        </AsyncLogger>-->

        <Root level="warn">
            <!--            <AppenderRef ref="Console"/>-->
            <AppenderRef ref="WarningFile"/>
            <AppenderRef ref="ErrorFile"/>
        </Root>
    </Loggers>
</Configuration>

参考