您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天就跟大家聊聊有关如何在springboot项目中配置log4j.yml日志文件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
1, 去掉默认日志,以便切换到log4j2的日志依赖
2, 然后添加如下两个日志依赖
2, 配置文件内容
<!-- 配置 log4j2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- 加上这个才能辨认到log4j2.yml文件 --> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> </dependency>
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.path}/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
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()); }
看完上述内容,你们对如何在springboot项目中配置log4j.yml日志文件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。