赞
踩
在多线程情况下,使用log4j可能会阻塞其他线程,从而导致整体性能下降并出现性能瓶颈。所以需要升级到性能更好并支持异步的log4j2。
- <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>
(1)替换import package
原来的包
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
更换为新包
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
(2)替换logger新接口
原来的日志类定义
private static final Logger LOGGER = LoggerFactory.getLogger(Atest.class);
private static final Logger LOGGER = LoggerFactory.getLogger("newFile");
更换为新日志类接口
private static final Logger LOGGER = LogManager.getLogger(Atest.class)
private static final Logger LOGGER = LogManager.getLogger("newFile");
- logging:
- config: classpath:log4j2.xml
- <?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>
log4j2.contextSelector=org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。