SpringBoot中使用Log4j2日志框架(简单使用)

文章记录使用过程中遇到的问题,下面是参考的文章

一、log4j2的使用

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文件


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值