文章记录使用过程中遇到的问题,下面是参考的文章
一、log4j2的使用
-
Log4j2简介和异步日志梳理 ——该文章全局异步和混合异步讲解的很清楚
-
log4j2的认知 ——该文章阐述了logger与root之间的关系以及它们如何执行的
-
Log4j2中的同步日志与异步日志 ——该文章AsyncAppender和AsyncLogger之间的区别讲的很清楚
-
log4j2 pattern详解 ——该文章讲述了日志输出形式的各种参数pattern
-
Log4j2 简明教程 和Log4j2配置详解 ——主要讲述了log4j2每个标签的含义和标签的属性
-
Springboot配置log4j2异步输出日志 ——主要讲述如何验证是否已开启全局异步日志
-
Log4j2高级配置以及简单的示例 ——主要讲述了不同Appender的含义以及用途
1、FileAppender 普通地输出到本地文件
2、FlumeAppender 将几个不同源的日志汇集、集中到一处。
3、JMSQueueAppender VS. JMSTopicAppender 与JMS相关的日志输出
4、RewriteAppender 对日志事件进行掩码或注入信息
5、RollingFileAppender 对日志文件进行封存(详细)
6、RoutingAppender 在输出地之间进行筛选路由
7、SMTPAppender 将LogEvent发送到指定邮件列表
8、SocketAppender 将LogEvent以普通格式发送到远程主机
9、SyslogAppender 将LogEvent以RFC 5424格式发送到远程主机
10、AsynchAppender 将一个LogEvent异步地写入多个不同输出地
11、ConsoleAppender 将LogEvent输出到命令行
12、FailoverAppender 维护一个队列,系统将尝试向队列中的Appender依次输出LogEvent,直到有一个成功为止
二、配置控制台打印彩色日志
Springboot整合log4j2-问题与配置(idea打印彩色日志信息)
在Log4j 2.10以前的版本,pattern中配置%highlight属性是可以正常打印彩色日志的。
但是是更新到2.10版本以后,控制台中就无法显示彩色日志了,各种级别的日志混杂在一起,难以阅读。Log4j2默认关闭了Jansi(一个支持输出ANSI颜色的类库)。IDEA中,点击右上角->Edit Configurations,在VM options中
添加 -Dlog4j.skipJansi=false
打印彩色日志,除了上面加的VM参数外,还要再pattern中添加修饰,下面是一个例子,具体的看上面那篇文章,里面有讲
< Property name=“CONSOLE”>[%style{%d{yyyy-MM-dd HH:mm:ss:SSS}}{bright,green}] | [%highlight{%5level}] [%thread] [%style{%c}{bright,yellow}] [%style{%L}{bright,blue}] -| %highlight{%msg}%n%style{%throwable}{red}< /Property>
三、学习使用log4j2遇到的问题
1、全局排除springboot默认日志依赖
项目中引入了mybatis等starter,如mybatis-spring-boot-starter、spring-boot-starter-validation,发现这些依赖里都用了springboot默认的日志,而网上很多文章都只是说了要排除spring-boot-starter-web里面的默认日志依赖,参考文章——Spring Boot 全局排除 spring-boot-starter-logging 依赖 ,可以全局排除默认的日志依赖。
如果没有全局去除的话会出现下面警告
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/mavenJarOnline/ch/qos/logback/logback-classic/1.1.9/logback-classic-1.1.9.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/mavenJarOnline/org/slf4j/slf4j-log4j12/1.7.22/slf4j-log4j12-1.7.22.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
<!--全局排除spring-boot-starter-logging内的所有依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
1、背景
- springboot2.3.5.RELEASE
- maven项目
- jdk1.8
2、添加依赖(末尾有完整的pom.xml)
<!-- 引入log4j2依赖 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 这是开启异步log4j2需要引入的依赖 -->
<!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
3、在resources目录下添加log4j.xml文件