springboot log4j upgrade log4j2

Preface

In multi-threaded situations, using log4j may block other threads, resulting in overall performance degradation and performance bottlenecks. Therefore, it is necessary to upgrade to log4j2 with better performance and support for asynchronous.

Upgrade steps

1. Update maven pom dependencies

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.2</version>
        </dependency>

2.Update code

(1) Replace import package

original bag

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Replace with the new package
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

(2) Replace the new interface of logger

Original log class definition

private static final Logger LOGGER = LoggerFactory.getLogger(Atest.class);

private static final Logger LOGGER = LoggerFactory.getLogger("newFile");

Replace with the new log class interface
private static final Logger LOGGER = LogManager.getLogger(Atest.class)

private static final Logger LOGGER = LogManager.getLogger("newFile");

3. Specify the log4j2 log configuration file in application.yml

logging:
  config: classpath:log4j2.xml

4. Add the log configuration file log4j2.xml under src/main/resources

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Properties>
        <Property name="system.logPath">./log</Property>
    </Properties>

    <Appenders>
        <!-- console -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{ISO8601} %c{1} %p - %m%n"/>
        </Console>

        <!-- info日志文件 -->
        <RollingFile name="InfoFile" fileName="${sys:system.logPath}/info.log"
                     filePattern="${sys:system.logPath}/info-%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="%d{ISO8601} %c{1} %p - %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="2"/>
        </RollingFile>

        <!-- newFile日志文件 -->
        <RollingFile name="NewFile" fileName="${sys:system.logPath}/NewFile.log"
                     filePattern="${sys:system.logPath}/NewFile-%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="%d{MM-dd_HH:mm:ss} %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="128 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="2"/>
        </RollingFile>

    </Appenders>

    <Loggers>

        <!-- 设置特定路径的日志级别 -->
        <Logger name="org.apache.kafka" level="info"/>

        <!-- rootLogger定义 -->
        <Root level="INFO">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="InfoFile"/>
        </Root>

        <!-- newFile日志专用logger -->
        <Logger name="newFile" level="INFO" additivity="true">
            <AppenderRef ref="NewFile"/>
        </Logger>
    
    </Loggers>
</Configuration>

5. Add the log configuration file log4j2.component.properties under src/main/resources and start asynchronous configuration

log4j2.contextSelector=org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector

Test whether the log4j2 asynchronous configuration takes effect

Reference: Test whether the springboot log4j2 asynchronous configuration is effective_Mint6's blog-CSDN blog

Guess you like

Origin blog.csdn.net/Mint6/article/details/130660987