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

时间:2022-11-22 08:18:11

一,Maven 依赖 pom.xml配置

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

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

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

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

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

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

2, 配置文件内容

  1. <!-- 配置 log4j2 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-log4j2</artifactId>
  5. </dependency>
  6.  
  7. <!-- 加上这个才能辨认到log4j2.yml文件 -->
  8. <dependency>
  9. <groupId>com.fasterxml.jackson.dataformat</groupId>
  10. <artifactId>jackson-dataformat-yaml</artifactId>
  11. </dependency>

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

1, 文件存放位置

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

2, 配置文件内容

  1. # 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。
  2. Configuration:
  3. status: warn
  4. monitorInterval: 30
  5. Properties: # 定义全局变量
  6. Property: # 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下:
  7. #测试:-Dlog.level.console=warn -Dlog.level.xjj=trace
  8. #生产:-Dlog.level.console=warn -Dlog.level.xjj=info
  9. - name: log.level.console
  10. value: info
  11. - name: log.path
  12. value: log
  13. - name: project.name
  14. value: opendoc
  15. - name: log.pattern
  16. value: "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-30.30C{1.} : %m%n"
  17. Appenders:
  18. Console: #输出到控制台
  19. name: CONSOLE
  20. target: SYSTEM_OUT
  21. PatternLayout:
  22. pattern: ${log.pattern}
  23. # 启动日志
  24. RollingFile:
  25. - name: ROLLING_FILE
  26. fileName: ${log.path}/${project.name}.log
  27. filePattern: "${log.path}/historyRunLog/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
  28. PatternLayout:
  29. pattern: ${log.pattern}
  30. Filters:
  31. # 一定要先去除不接受的日志级别,然后获取需要接受的日志级别
  32. ThresholdFilter:
  33. - level: error
  34. onMatch: DENY
  35. onMismatch: NEUTRAL
  36. - level: info
  37. onMatch: ACCEPT
  38. onMismatch: DENY
  39. Policies:
  40. TimeBasedTriggeringPolicy: # 按天分类
  41. modulate: true
  42. interval: 1
  43. DefaultRolloverStrategy: # 文件最多100个
  44. max: 100
  45. # 平台日志
  46. - name: PLATFORM_ROLLING_FILE
  47. ignoreExceptions: false
  48. fileName: ${log.path}/platform/${project.name}_platform.log
  49. filePattern: "${log.path}/platform/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
  50. PatternLayout:
  51. pattern: ${log.pattern}
  52. Policies:
  53. TimeBasedTriggeringPolicy: # 按天分类
  54. modulate: true
  55. interval: 1
  56. DefaultRolloverStrategy: # 文件最多100个
  57. max: 100
  58. # 业务日志
  59. - name: BUSSINESS_ROLLING_FILE
  60. ignoreExceptions: false
  61. fileName: ${log.path}/bussiness/${project.name}_bussiness.log
  62. filePattern: "${log.path}/bussiness/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
  63. PatternLayout:
  64. pattern: ${log.pattern}
  65. Policies:
  66. TimeBasedTriggeringPolicy: # 按天分类
  67. modulate: true
  68. interval: 1
  69. DefaultRolloverStrategy: # 文件最多100个
  70. max: 100
  71. # 错误日志
  72. - name: EXCEPTION_ROLLING_FILE
  73. ignoreExceptions: false
  74. fileName: ${log.path}/exception/${project.name}_exception.log
  75. filePattern: "${log.path}/exception/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
  76. ThresholdFilter:
  77. level: error
  78. onMatch: ACCEPT
  79. onMismatch: DENY
  80. PatternLayout:
  81. pattern: ${log.pattern}
  82. Policies:
  83. TimeBasedTriggeringPolicy: # 按天分类
  84. modulate: true
  85. interval: 1
  86. DefaultRolloverStrategy: # 文件最多100个
  87. max: 100
  88. # DB 日志
  89. - name: DB_ROLLING_FILE
  90. ignoreExceptions: false
  91. fileName: ${log.path}/db/${project.name}_db.log
  92. filePattern: "${log.path}/db/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
  93. PatternLayout:
  94. pattern: ${log.pattern}
  95. Policies:
  96. TimeBasedTriggeringPolicy: # 按天分类
  97. modulate: true
  98. interval: 1
  99. DefaultRolloverStrategy: # 文件最多100个
  100. max: 100
  101. Loggers:
  102. Root:
  103. level: info
  104. AppenderRef:
  105. - ref: CONSOLE
  106. - ref: ROLLING_FILE
  107. - ref: EXCEPTION_ROLLING_FILE
  108. Logger:
  109. - name: platform
  110. level: info
  111. additivity: false
  112. AppenderRef:
  113. - ref: CONSOLE
  114. - ref: PLATFORM_ROLLING_FILE
  115. - name: bussiness
  116. level: info
  117. additivity: false
  118. AppenderRef:
  119. - ref: BUSSINESS_ROLLING_FILE
  120. - name: exception
  121. level: debug
  122. additivity: true
  123. AppenderRef:
  124. - ref: EXCEPTION_ROLLING_FILE
  125. - name: db
  126. level: info
  127. additivity: false
  128. AppenderRef:
  129. - ref: DB_ROLLING_FILE
  130. # 监听具体包下面的日志
  131. # Logger: # 为com.xjj包配置特殊的Log级别,方便调试
  132. # - name: com.xjj
  133. # additivity: false
  134. # level: ${sys:log.level.xjj}
  135. # AppenderRef:
  136. # - ref: CONSOLE
  137. # - ref: ROLLING_FILE

3, 在application.yml引入log4j2.yml

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

四, 不同日志枚举类

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

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

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

  1. package com.tcs.irecr.util;
  2.  
  3. import org.slf4j.Logger;
  4.  
  5. import org.slf4j.LoggerFactory;
  6.  
  7. /**
  8.  
  9. * 本地日志参考类 *
  10.  
  11. @author Administrator *
  12.  
  13. */
  14.  
  15. public class LogUtils {
  16. //* 获取业务日志logger
  17.  
  18. }
  19.  
  20. public static Logger getBussinessLogger() {
  21. return LoggerFactory.getLogger(LogEnum.BUSSINESS.getCategory());
  22.  
  23. }
  24.  
  25. //获取平台日志logger
  26.  
  27. public static Logger getPlatformLogger() {
  28. return LoggerFactory.getLogger(LogEnum.PLATFORM.getCategory());
  29.  
  30. }
  31.  
  32. //获取数据库日志logger
  33.  
  34. public static Logger getDBLogger() {
  35. return LoggerFactory.getLogger(LogEnum.DB.getCategory());
  36.  
  37. }
  38.  
  39. //获取异常日志logger
  40.  
  41. public static Logger getExceptionLogger() {
  42. return LoggerFactory.getLogger(LogEnum.EXCEPTION.getCategory());
  43.  
  44. }

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

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

到此这篇关于springboot+log4j.yml配置日志文件的方法的文章就介绍到这了,更多相关springboot log4j.yml配置日志内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_38319425/article/details/93646428