springboot配置log4j

使用

Logger logger=LogManager.getLogger(this.getClass());

logger.info(“hello”);

log4j

Loggers: 记录器,有7个级别,只输出级别不低于设定级别的日志,all<debug<info<warn<error<fatal<off

Appenders: 输出源,控制台Console、文件Files

Layouts: 布局,日志信息的输出方式,格式化输出的信息

pom

log4j
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<dependency>

识别yml
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.3</version>
</dependency>

去掉spring-boot的日志包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</dependency>

去掉mybatis的日志包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging></artifactId>
</exclusion>
</exclusions>
</dependency>

log4j2.yml

Configuration:
status: warn
Properties: # 定义全局变量
Property: # 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下:
#测试:-Dlog.level.console=warn -Dlog.level.xjj=trace
#生产:-Dlog.level.console=warn -Dlog.level.xjj=info
– name: log.level.console
value: trace
– name: log.level.xjj
value: trace
– name: log.path
value: logs
– name: project.name
value: my-spring-boot
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}:%4p %t (%F:%L) – %m%n”
RollingFile: # 输出到文件,超过128MB归档
– name: ROLLING_FILE
ignoreExceptions: false
fileName: ${log.path}/${project.name}.log
filePattern: “${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz”
PatternLayout:
pattern: “%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) – %m%n”
Policies:
SizeBasedTriggeringPolicy:
size: “128 MB”
DefaultRolloverStrategy:
max: 1000
– name: EXCEPTION
ignoreExceptions: false
fileName: ${log.path}/exception.log
filePattern: “${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz”
PatternLayout:
pattern: “%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) – %m%n”
Policies:
SizeBasedTriggeringPolicy:
size: “128 MB”
DefaultRolloverStrategy:
max: 1000
– name: WEB_REQUEST
ignoreExceptions: false
fileName: ${log.path}/web_request.log
filePattern: “${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz”
PatternLayout:
pattern: “%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) – %m%n”
Policies:
SizeBasedTriggeringPolicy:
size: “128 MB”
DefaultRolloverStrategy:
max: 1000
– name: MYBATIS_SQL
ignoreExceptions: false
fileName: ${log.path}/mybatis_sql.log
filePattern: “${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz”
PatternLayout:
pattern: “%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) – %m%n”
Policies:
SizeBasedTriggeringPolicy:
size: “128 MB”
DefaultRolloverStrategy:
max: 1000
– name: TIME_TASK
ignoreExceptions: false
fileName: ${log.path}/time_task.log
filePattern: “${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz”
PatternLayout:
pattern: “%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) – %m%n”
Policies:
SizeBasedTriggeringPolicy:
size: “128 MB”
DefaultRolloverStrategy:
max: 1000
– name: ASYNC_TASK
ignoreExceptions: false
fileName: ${log.path}/async_task.log
filePattern: “${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz”
PatternLayout:
pattern: “%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) – %m%n”
Policies:
SizeBasedTriggeringPolicy:
size: “128 MB”
DefaultRolloverStrategy:
max: 1000
Loggers:
Root:
level: info
AppenderRef:
– ref: CONSOLE
– ref: ROLLING_FILE
Logger:
– name: WEB_REQUEST_LOG
additivity: false
level: info
AppenderRef:
– ref: WEB_REQUEST
– name: com.example.demo.business #打印mytabis的sql执行日志
additivity: false
level: trace
AppenderRef:
– ref: MYBATIS_SQL
– name: TIME_TASK_LOG
additivity: false
level: trace
AppenderRef:
– ref: TIME_TASK
– name: ASYNC_TASK_LOG
additivity: false
level: trace
AppenderRef:
– ref: ASYNC_TASK
– name: EXCEPTION_LOG
additivity: false
level: trace
AppenderRef:
– ref: EXCEPTION

log4j2.xml

logging.config=classpath:log4j2.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<Configuration status=”WARN”>
<appenders>
<Console name=”Console” target=”SYSTEM_OUT”>
<PatternLayout pattern=”[%d{HH:mm:ss:SSS}] [%p] – %l – %m%n”/>
</Console>
<RollingFile name=”RollingFileInfo” fileName=”C:/info.log” filePattern=”C:/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log”>
<Filters>
<ThresholdFilter level=”INFO”/>
</Filters>
<PatternLayout pattern=”[%d{HH:mm:ss:SSS}] [%p] -%l – %m%n”/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size=”100MB”/>
</Policies>
</RollingFile>
</appenders>
<loggers>
<root level=”info”>
<appender-ref ref=”Console”/>
<appender-ref ref=”RollingFileInfo”/>
</root>
</loggers>
</Configuration>

参考资料

https://www.cnblogs.com/vitasyuan/p/8783838.html

《一步一步学Spring Boot2 微服务项目实战》 黄文毅