首先假设读者已经将SpringBoot+Idea+Maven工程成功搭建,下面对Log4j2的集成做一下说明:
# log4j2.xml配置
logging:
config: classpath:log4j2.xml
# log4j2.yml配置
# logging:
# config: classpath:log4j2.properties
# log4j2.yml配置
# logging:
# config: classpath:log4j2.yml
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
com.fasterxml.jackson.dataformat
jackson-dataformat-yaml
若添加依赖后Maven不能自动加载,则需要我们辅助安装一下,操作如下图所示:
在配置依赖时需要注意:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
(1) Log4j2.xml
(2)Log4j2.properties:
### 设置###
log4j.rootLogger = debug,stdout,D,E
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出DEBUG 级别以上的日志到=G://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = G://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %l] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=G://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =G://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%l] - [ %p ] %m%n
(3)Log4j2.yml:
# 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。
Configuration:
status: OFF
name: YAMLConfigTest
monitorInterval: 30
Properties: # 定义全局变量
Property: # 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下:
#测试:-Dlog.level.console=warn -Dlog.level.xjj=trace
#生产:-Dlog.level.console=warn -Dlog.level.xjj=info
- name: log.level.console
value: INFO
- name: log.path
value: /export/Logs/
- name: file.name
value: BankManager
- name: log.patternA
value: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level traceId=[%X{traceId}] %logger{36} - %msg%n"
thresholdFilter:
level: debug
appenders:
#只接受程序中DEBUG级别的日志进行处理
Console:
name: Console
target: SYSTEM_OUT
ThresholdFilter:
level: DEBUG
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
Pattern: "[%d{HH:mm:ss.SSS}] %-5level %class{36} %L %M - %msg%xEx%n"
#处理DEBUG级别的日志,并把该日志放到logs/debug.log文件中
RollingFile:
#打印出DEBUG级别日志,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档
-
name: RollingFileDebug
fileName: ./logs/debug.log
filePattern: "logs/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
Pattern: "[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"
Filters:
ThresholdFilter:
-
level: DEBUG
-
level: INFO
onMatch: DENY
onMismatch: NEUTRAL
Policies:
SizeBasedTriggeringPolicy:
size: 512MB
#处理INFO级别的日志,并把该日志放到logs/info.log文件中
-
name: RollingFileInfo
fileName: ./logs/info.log
filePattern: "logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
Pattern: "[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"
Filters:
#只接受INFO级别的日志,其余的全部拒绝处理
ThresholdFilter:
-
level: INFO
-
level: WARN
onMatch: DENY
onMismatch: NEUTRAL
Policies:
SizeBasedTriggeringPolicy:
size: 512MB
#处理WARN级别的日志,并把该日志放到logs/warn.log文件中
-
name: RollingFileWarn
fileName: ./logs/warn.log
filePattern: "logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
Pattern: "[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"
Filters:
ThresholdFilter:
-
level: WARN
-
level: ERROR
onMatch: DENY
onMismatch: NEUTRAL
Policies:
SizeBasedTriggeringPolicy:
size: 512MB
#处理error级别的日志,并把该日志放到logs/error.log文件中
-
name: RollingFileError
fileName: ./logs/error.log
filePattern: "logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
Pattern: "[%d{yyyy-MM-dd HH:mm:ss}] %-5level %class{36} %L %M - %msg%xEx%n"
ThresholdFilter:
level: ERROR
Policies:
SizeBasedTriggeringPolicy:
size: 512MB
#druid的日志记录追加器
-
name: druidSqlRollingFile
fileName: ./logs/druid-sql.log
filePattern: "logs/$${date:yyyy-MM}/api-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
Pattern: "[%d{yyyy-MM-dd HH:mm:ss}] %-5level %L %M - %msg%xEx%n"
Policies:
SizeBasedTriggeringPolicy:
size: 512MB
loggers:
logger:
#记录druid-sql的记录
name: druid.sql.Statement
level: debug
additivity: false
appender-ref:
ref: druidSqlRollingFile
#log4j2 自带过滤日志
Logger:
-
name: org.apache.catalina.startup.DigesterFactory
level: error
-
name: org.apache.catalina.util.LifecycleBase
level: error
-
name: org.apache.coyote.http11.Http11NioProtocol
level: warn
-
name: org.apache.sshd.common.util.SecurityUtils
level: warn
-
name: org.apache.tomcat.util.net.NioSelectorPool
level: warn
-
name: org.crsh.plugin
level: warn
-
name: org.crsh.ssh
level: warn
-
name: org.eclipse.jetty.util.component.AbstractLifeCycle
level: error
-
name: org.hibernate.validator.internal.util.Version
level: warn
-
name: org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration
level: warn
-
name: org.springframework.boot.actuate.endpoint.jmx
level: warn
-
name: org.thymeleaf
level: warn
root:
level: DEBUG
appender-ref:
-
ref: Console
-
ref: RollingFileInfo
-
ref: RollingFileWarn
-
ref: RollingFileError
-
ref: RollingFileDebug
具体在编程中进行日志的添加前还需要在类的开头添加如下注解:
@Slf4j
注:在使用Slf4j日志接口进行操作时还需要注意一个问题,log4j2中日志级别从大到小依次是: off, fatal, error, warn, info, debug, trace, all,但是Slf4j这个接口包中并未提供对红色标注的日志级别的输出,所以在配置时还需要在下面这个位置注意,不能写红色级别的,否则工程加载过程会报错。
Configuration:
status: OFF
至此就可以正常使用Log4j2的日志功能了。