第一种:基于log4j2.properties
首先pom.xml中导入jar
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
然后在src/main/resources目录下新建log4j2.properties,该日志文件可以实现,每天新建一个日志文件,前一天的日志以前一天的日期为结尾
### \u8BBE\u7F6E###
log4j.rootLogger = debug,stdout,D,E,I
### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.encoding=utf-8
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D://logs/debug.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
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,SSS} %5p %c{1}:%L - %m%n
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =D://logs/error.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
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,SSS} %5p %c{1}:%L - %m%n
log4j.appender.I = org.apache.log4j.DailyRollingFileAppender
log4j.appender.I.File =D://logs/info.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.I.Append = true
log4j.appender.I.Threshold = INFO
log4j.appender.I.layout = org.apache.log4j.PatternLayout
log4j.appender.I.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
第二种方式:基于log4j2.yml配置文件
首先pom.xml中导入jar
<dependency> <!-- exclude掉spring-boot的默认log配置 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入log4j2依赖 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml -->
<dependency><!-- 加上这个才能辨认到log4j2.yml文件 -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
然后在src/main/resources目录下新建log4j2.yml文件
# 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。
Configuration:
status: warn
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: debug
- name: log.sql.level
value: trace
- name: LOG_HOME
value: /bdmsweb-log/logs
Appenders:
Console: #输出到控制台
name: CONSOLE
target: SYSTEM_OUT
ThresholdFilter:
level: ${sys:log.level.console} # “sys:”表示:如果VM参数中没指定这个变量值,则使用本文件中定义的缺省全局变量值
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
RollingFile: # 输出到文件,超过128MB归档
- name: info
ignoreExceptions: false
fileName: ${LOG_HOME}/${date:yyyy-MM}/${date:yyyy-MM-dd}/info.log
filePattern: "${LOG_HOME}/${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"
ThresholdFilter:
level: info
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
Policies:
SizeBasedTriggeringPolicy:
size: "128 MB"
DefaultRolloverStrategy:
max: 1000
- name: debug
ignoreExceptions: false
fileName: ${LOG_HOME}/${date:yyyy-MM}/${date:yyyy-MM-dd}/debug.log
filePattern: "${LOG_HOME}/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log.gz"
ThresholdFilter:
level: debug
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
Policies:
SizeBasedTriggeringPolicy:
size: "128 MB"
DefaultRolloverStrategy:
max: 1000
- name: error
ignoreExceptions: false
fileName: ${LOG_HOME}/${date:yyyy-MM}/${date:yyyy-MM-dd}/error.log
filePattern: "${LOG_HOME}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz"
ThresholdFilter:
level: error
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
Policies:
SizeBasedTriggeringPolicy:
size: "128 MB"
DefaultRolloverStrategy:
max: 1000
Loggers:
Root:
level: info
AppenderRef:
- ref: CONSOLE
- ref: info
- ref: debug
- ref: error
Logger: # 为com.xjj包配置特殊的Log级别,方便调试
- name: cnki.bdms.module.search.dal
additivity: true
level: ${sys:log.sql.level}
AppenderRef:
- ref: info
- ref: debug
- ref: error
- ref: CONSOLE
最后在application.yml中添加配置
logging:
config: classpath:log4j2.yml
完成以上配置之后 运行项目,便可以在D盘/bdmsweb-log/logs 目录下看到以日期为区分的日志文件
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_43842093/article/details/122810795
内容来源于网络,如有侵权,请联系作者删除!