当前位置:   article > 正文

springboot log4j升级log4j2

log4j升级

前言

在多线程情况下,使用log4j可能会阻塞其他线程,从而导致整体性能下降并出现性能瓶颈。所以需要升级到性能更好并支持异步的log4j2

升级步骤

1.更新maven pom依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-logging</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-log4j2</artifactId>
  14. <version>3.0.3</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.lmax</groupId>
  18. <artifactId>disruptor</artifactId>
  19. <version>3.4.2</version>
  20. </dependency>

2.更新代码

(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");

3.在application.yml指定log4j2日志配置文件

  1. logging:
  2. config: classpath:log4j2.xml

4.在src/main/resources下新增日志配置文件log4j2.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="INFO">
  3. <Properties>
  4. <Property name="system.logPath">./log</Property>
  5. </Properties>
  6. <Appenders>
  7. <!-- console -->
  8. <Console name="Console" target="SYSTEM_OUT">
  9. <PatternLayout pattern="%d{ISO8601} %c{1} %p - %m%n"/>
  10. </Console>
  11. <!-- info日志文件 -->
  12. <RollingFile name="InfoFile" fileName="${sys:system.logPath}/info.log"
  13. filePattern="${sys:system.logPath}/info-%d{yyyy-MM-dd}.log">
  14. <PatternLayout pattern="%d{ISO8601} %c{1} %p - %m%n"/>
  15. <Policies>
  16. <SizeBasedTriggeringPolicy size="100 MB"/>
  17. </Policies>
  18. <DefaultRolloverStrategy max="2"/>
  19. </RollingFile>
  20. <!-- newFile日志文件 -->
  21. <RollingFile name="NewFile" fileName="${sys:system.logPath}/NewFile.log"
  22. filePattern="${sys:system.logPath}/NewFile-%d{yyyy-MM-dd}.log">
  23. <PatternLayout pattern="%d{MM-dd_HH:mm:ss} %m%n"/>
  24. <Policies>
  25. <SizeBasedTriggeringPolicy size="128 MB"/>
  26. </Policies>
  27. <DefaultRolloverStrategy max="2"/>
  28. </RollingFile>
  29. </Appenders>
  30. <Loggers>
  31. <!-- 设置特定路径的日志级别 -->
  32. <Logger name="org.apache.kafka" level="info"/>
  33. <!-- rootLogger定义 -->
  34. <Root level="INFO">
  35. <AppenderRef ref="Console"/>
  36. <AppenderRef ref="InfoFile"/>
  37. </Root>
  38. <!-- newFile日志专用logger -->
  39. <Logger name="newFile" level="INFO" additivity="true">
  40. <AppenderRef ref="NewFile"/>
  41. </Logger>
  42. </Loggers>
  43. </Configuration>

5.在src/main/resources下新增日志配置文件log4j2.component.properties,启动异步配置

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

测试log4j2异步配置是否生效

参考:测试springboot log4j2异步配置是否生效_Mint6的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/547995
推荐阅读
相关标签
  

闽ICP备14008679号