SpringBoot快速入门-配置Lombok+Log4j2日志输出功能

1、引入lombok

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
</dependency>

2、引入Log4j2

<!-- 引入log4j2依赖 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

3、在要使用日志的类上添加注释:@Log4j2
代码编译时,会帮我们生成类似的代码:private static final Logger log = LoggerFactory.getLogger(TestLog2.class);

4、resource目录下添加文件log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="180" packages="">
    <properties>
		<!-- 日志输出文件名,这里配置的是从SpringBoot的yml中取配置来命名日志文件 -->
        <property name="app.log">itzhimei-ms-${server.port:-8081}</property>
		<!-- 日志输出目录 -->
        <property name="LOG_HOME">/opt/logs/${server.log}</property>
		<!--输出日志的格式-->
        <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p [%X{traceId}] %C %t (%F:%L) - %m%n</property>
    </properties>

    <Appenders>
        <!--Console -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${PATTERN}" />
        </Console>
        <RollingFile name="RollingFile" fileName="${LOG_HOME}.log"
                     filePattern="${LOG_HOME}.log.%d{yyyy-MM-dd}" append="true"
                     bufferedIO="true">
            <PatternLayout pattern="${PATTERN}" />
            <Filters>
				<!-- 设置控制台只输出INFO及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="NEUTRAL" />
                <StringMatchFilter text="feign.slf4j.Slf4jLogger" onMatch="ACCEPT" onMismatch="DENY" />
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>

        <!--异步appender -->
        <Async name="AsyncAppender" includeLocation="true">
            <AppenderRef ref="RollingFile" />
        </Async>

    </Appenders>

    <Loggers>
        <!--过滤掉spring和hibernate的一些无用的debug信息-->
        <logger name="org.springframework" level="INFO"/>
        <logger name="org.mybatis" level="INFO">
            <!-- 添加如下设置,控制台会再打印一次 -->
            <AppenderRef ref="Console"/>
        </logger>
        <root level="INFO">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileWarn"/>
            <appender-ref ref="RollingFileError"/>
        </root>

    </Loggers>
</Configuration>

5、application.yml添加配置

# Logger config
logging:
  config: classpath:log4j2.xml

6、测试代码及输出

/**
 * www.itzhimei.com
 * Lombok + Log4j日志配置
 */
@Log4j2
public class TestLog2 {

    //private static final Logger log = LoggerFactory.getLogger(TestLog2.class);

    public static void main(String[] args) {
        log.info("test lombok log");
        try {
            throw new Exception();
        } catch (Exception e) {
            log.error("lombok log-{}", "testErrBBB", e);
        }
    }
}

/* 输出
2022-11-21 14:56:50,899:INFO [] com.itzhimei.handle.TestLog2 main (TestLog2.java:18) - test lombok log
2022-11-21 14:56:50,902:ERROR [] com.itzhimei.handle.TestLog2 main (TestLog2.java:22) - lombok log-testErrBBB
java.lang.Exception: null
	at com.itzhimei.handle.TestLog2.main(TestLog2.java:20) [classes/:?]
 */