目录
- 一 为何要替换成log4j2
- 1.1 log4j2的优点
- 1.2 log4j2.XML如何读取application.yml中属性值
- 1.2.1 操作步骤
- 1.3 log4j&slf4j&log4j2需要的导入的包
- 1.4 整合log4j2需要引入的依赖与排除依赖
一 为何要替换成log4j2
1.1 log4j2的优javascript点
log4j2使用了两种方式记录日志:AsyncAppender和AsyncLogger。
1.AsyncAppender使用队列异步记录日志,但是一旦队列已满,appender线程需要等待。
2.AsyncLogger是采用Disruptor,通过环形队列无阻塞队列作为缓冲,多生产者多线程的竞争是通过CAS实现,无锁化实现,可以降低极端大的日志量时候的延迟尖峰,Disruptor 可是号称一个线程里每秒处理600万订单的高性能队列。注意的是: 使用AsyncLogger才会使用disruptor提高性能。如果使用的AsyncAppender,则使用的是类似logback一样的队列的方式做异步记录。
1.2 log4j2.xml如何读取application.yml中属性值
1.2.1 操作步骤
1.在application.properties 或者application.yml文件中配置
patcher.log=/data350/fengqxLogFile/patcher
2.编写监听器
@Component public class LoggerListener impleandroidments ApplicationListener<ApplicationEvent>, Ordered { js@Override public void onApplicationEvent(ApplicationEvent applicationEvent) { if (applicationEvent instanceof ApplicationEnvironmentPreparedEvent) { ConfigurableEnvironment environment = ((ApplicationEnvironmentPreparedEvent) applicationEvent).getEnvironment(); String filePath = environment.getProperty("patcher.log"); if (StringUtils.isNotEmpty(filePath)) { System.err.println("=================" + filePath); System.setPropert编程客栈y("patcher.log", filePath); } } } @Override public int getOrder() { // 当前监听器的启动顺序需要在日志配置监听器的前面,所以此处减 1 return LoggingApplicationListener.DEFAULT_ORDER - 1; }
3.在 /src/main/resources/META-INF/spring.factories进行加载监听器
org.springframework.context.ApplicationListener=com.longze.patcher.listener.MyLoggerListener
4.log4j2.xml进行使用配置是路径
1.3 log4j&slf4j&log4j2需要的导入的包
Log4j实例:
import org.apache.log4j.Logger; private static final Logger LOGGER = Logger.getLogger(X.class);
Slf4j实例:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger logger = LoggerFactory.getLogger(X.class);
Log4j2的使用:
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; private static final Logger logger = LogManager.getLogger(X.class);
1.4 整合log4j2需要引入的依赖与排除依赖
1.引入依赖
<dependencies> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.17.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.17.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency> <groupId>org.apache.logging.log4jjavascript</groupId> <artifactId>log4j-core</artifactId> <version>2.17.1</version> </dependency> <dependency> <groupId>com.Imax</groupId> <artifactId>disruptor</artifactId> <version>2.17.1</version> </dependency> </dependencies>
排除jar包:
到此这篇关于SpringBoot将logback替换成log4j2的操作步骤的文章就介绍到这了,更多相关SpringBoot logback替换成log4j2内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论