springboot+log4j.yml配置日志文件的方法

网友投稿 1270 2023-02-13

springboot+log4j.yml配置日志文件的方法

springboot+log4j.yml配置日志文件的方法

一,Maven 依赖 pom.xml配置

1, 去掉默认日志,以便切换到log4j2的日志依赖

2, 然后添加如下两个日志依赖

二,在工程根目录下添加 lo4g2.yml 配置文件

2, 配置文件内容

org.springframework.boot

spring-boot-starter-log4j2

com.fasterxml.jackson.dataformat

jackson-dataformat-yaml

三,在工程根目录下添加 lo4g2.yml 配置文件

1, 文件存放位置

2, 配置文件内容

# 共有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: info

- name: log.path

value: log

- name: project.name

value: opendoc

- name: log.pattern

value: "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-30.30C{1.} : %m%n"

Appenders:

Console: #输出到控制台

name: CONSOLE

target: SYSTEM_OUT

PatternLayout:

pattern: ${log.pattern}

# 启动日志

RollingFile:

- name: ROLLING_FILE

fileName: ${log.path}/${project.name}.log

filePattern: "${log.path}/historyRunLog/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"

PatternLayout:

pattern: ${log.pattern}

Filters:

# 一定要先去除不接受的日志级别,然后获取需要接受的日志级别

ThresholdFilter:

- level: error

onMatch: DENY

onMismatch: NEUTRAL

- level: info

onMatch: ACCEPT

onMismatch: DENY

Policies:

TimeBasedTriggeringPolicy: # 按天分类

modulate: true

interval: 1

DefaultRolloverStrategy: # 文件最多100个

max: 100

# 平台日志

- name: PLATFORM_ROLLING_FILE

ignoreExceptions: false

fileName: ${log.path}/platform/${project.name}_platform.log

filePattern: "${log.path}/platform/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"

PatternLayout:

pattern: ${log.pattern}

Policies:

TimeBasedTriggeringPolicy: # 按天分类

modulate: true

interval: 1

DefaultRolloverStrategy: # 文件最多100个

max: 100

# 业务日志

- name: BUSSINESS_ROLLING_FILE

ignoreExceptions: false

fileName: ${log.path}/bussiness/${project.name}_bussiness.log

filePattern: "${log.path}/bussiness/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"

PatternLayout:

pattern: ${log.pattern}

Policies:

TimeBasedTriggeringPolicy: # 按天分类

modulate: true

interval: 1

DefaultRolloverStrategy: # 文件最多100个

max: 100

# 错误日志

- name: EXCEPTION_ROLLING_FILE

ignoreExceptions: false

fileName: ${log.path}/exception/${project.name}_exception.log

filePattern: "${log.patRwjlTsqOsIh}/exception/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"

ThresholdFilter:

level: error

onMatch: ACCEPT

onMismatch: DENY

PatternLayout:

pattern: ${log.pattern}

Policies:

TimeBasedTriggeringPolicy: # 按天分类

modulate: true

interval: 1

DefaultRolloverStrategy: # 文件最多100个

max: 100

# DB 日志

- name: DB_ROLLING_FILE

ignoreExceptions: false

fileName: ${log.path}/db/${project.name}_db.log

filePattern: "${log.path}/db/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"

PatternLayout:

pattern: ${log.pattern}

Policies:

TimeBasedTriggeringPolicy: # 按天分类

modulate: true

interval: 1

DefaultRolloverStrategy: # 文件最多100个

max: 100

Loggers:

Root:

level: info

AppenderRef:

- ref: CONSOLE

- ref: ROLLING_FILE

- ref: EXCEPTION_ROLLING_FILE

Logger:

- name: platform

level: info

additivity: false

AppenderRef:

- ref: CONSOLE

- ref: PLATFORM_ROLLING_FILE

- name: bussiness

level: info

additivity: false

AppenderRef:

- ref: BUSSINESS_ROLLING_FILE

- name: exception

level: debug

additivity: true

AppenderRef:

- ref: EXCEPTION_ROLLING_FILE

- name: db

level: info

additivity: false

AppenderRef:

- ref: DB_ROLLING_FILE

# 监听具体包下面的日志

# Logger: # 为com.xjj包配置特殊的Log级别,方便调试

# - name: com.xjj

# additivity: false

# level: ${sys:log.level.xjj}

# AppenderRef:

# - ref: CONSOLE

# - ref: ROLLING_FILE

3, 在application.yml引入log4j2.yml

四, 不同日志枚举类

五, 不同日志工具类util编辑

package com.tcs.irecr.util;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

* 本地日志参考类 *

@author Administrator *

*/

public class LogUtils {

//* 获取业务日志logger

}

public static Logger getBussinessLogger() {

return LoggerFactory.getLogger(LogEnum.BUSSINESS.getCategory());

}

//获取平台日志logger

public static Logger getPlatformLogger() {

return LoggerFactory.getLogger(LogEnum.PLATFORM.getCategory());

}

//获取数据库日志logger

public static Logger getDBLogger() {

return LoggerFactory.getLogger(LogEnum.DB.getCategory());

}

//获取异常日志logger

public static Logger getExceptionLogger() {

return LoggerFactory.getLogger(LogEnum.EXCEPTION.getCategory());

}

六, 运行时自动在工程目录下生产日志目录和日志文件

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决IDEA光标变成白色粗条的问题
下一篇:解决IDEA鼠标点击光标变大问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~