logback(推荐)logback-spring.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration scan="true" scanPeriod="30 seconds">
  3. <contextListener
  4. class="ch.qos.logback.classic.jul.LevelChangePropagator" />
  5. <property name="App_Name" value="app" />
  6. <property name="Log_Path" value="logs/" />
  7. <property name="File_Pattern"
  8. value="%d{yyyy-MM-dd HH:mm:ss:SSS} %-5level [%t] %logger - %msg%n" />
  9. <property name="Console_pattern"
  10. value="%d{yyyy-MM-dd HH:mm:ss:SSS} %-5level [%t] %logger:%-4line - %msg%n" />
  11. <contextName>${App_Name}</contextName>
  12. <appender name="console"
  13. class="ch.qos.logback.core.ConsoleAppender">
  14. <encoder>
  15. <pattern>${Console_pattern}</pattern>
  16. </encoder>
  17. </appender>
  18. <appender name="infoRollingFile"
  19. class="ch.qos.logback.core.rolling.RollingFileAppender">
  20. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  21. <level>INFO</level>
  22. <onMatch>ACCEPT</onMatch>
  23. <onMismatch>DENY</onMismatch>
  24. </filter>
  25. <file>${Log_Path}${App_Name}.log</file>
  26. <rollingPolicy
  27. class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  28. <fileNamePattern>${Log_Path}${App_Name}-%d{yyyy-MM-dd}.%i.log
  29. </fileNamePattern>
  30. <maxFileSize>900MB</maxFileSize>
  31. <maxHistory>90</maxHistory>
  32. <totalSizeCap>10GB</totalSizeCap>
  33. </rollingPolicy>
  34. <encoder>
  35. <pattern>${File_Pattern}</pattern>
  36. </encoder>
  37. </appender>
  38. <appender name="errorRollingFile"
  39. class="ch.qos.logback.core.rolling.RollingFileAppender">
  40. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  41. <level>WARN</level>
  42. </filter>
  43. <file>${Log_Path}${App_Name}.error.log</file>
  44. <rollingPolicy
  45. class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  46. <fileNamePattern>${Log_Path}${App_Name}.error-%d{yyyy-MM-dd}.%i.log
  47. </fileNamePattern>
  48. <maxFileSize>900MB</maxFileSize>
  49. <maxHistory>90</maxHistory>
  50. <totalSizeCap>10GB</totalSizeCap>
  51. </rollingPolicy>
  52. <encoder>
  53. <pattern>${File_Pattern}</pattern>
  54. </encoder>
  55. </appender>
  56. <springProfile name="dev,local,zhlocal">
  57. <root level="INFO">
  58. <appender-ref ref="console" />
  59. <appender-ref ref="infoRollingFile" />
  60. <appender-ref ref="errorRollingFile" />
  61. </root>
  62. </springProfile>
  63. <springProfile name="test">
  64. <root level="INFO">
  65. <appender-ref ref="infoRollingFile" />
  66. <appender-ref ref="errorRollingFile" />
  67. </root>
  68. </springProfile>
  69. <springProfile name="demo,prod">
  70. <logger name="com.baomidou" level="WARN" />
  71. <root level="INFO">
  72. <appender-ref ref="infoRollingFile" />
  73. <appender-ref ref="errorRollingFile" />
  74. </root>
  75. </springProfile>
  76. </configuration>

log4j2

依赖配置(spring-boot)

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-logging</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-log4j2</artifactId>
  14. </dependency>

配置文件

log4j2.xml或log4j2-spring.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration monitorInterval="5">
  3. <!--变量配置 -->
  4. <Properties>
  5. <property name="LOG_PATTERN"
  6. value="%date{yyyy-MM-dd HH:mm:ss.SSS} [%-20.20thread] %-5level %-50.50logger{50} - %msg%n" />
  7. <property name="FILE_PATH" value="./logs" />
  8. <property name="FILE_NAME" value="zc-project" />
  9. </Properties>
  10. <appenders>
  11. <console name="Console" target="SYSTEM_OUT">
  12. <PatternLayout pattern="${LOG_PATTERN}" />
  13. <ThresholdFilter level="info" onMatch="ACCEPT"
  14. onMismatch="DENY" />
  15. </console>
  16. <File name="Filelog" fileName="${FILE_PATH}/${FILE_NAME}.log"
  17. append="false">
  18. <PatternLayout pattern="${LOG_PATTERN}" />
  19. </File>
  20. <RollingFile name="RollingFileInfo"
  21. fileName="${FILE_PATH}/${FILE_NAME}-info.log"
  22. filePattern="${FILE_PATH}/${FILE_NAME}-info-%d{yyyy-MM-dd}_%i.log">
  23. <PatternLayout pattern="${LOG_PATTERN}" />
  24. <Filters>
  25. <ThresholdFilter level="warn" onMatch="DENY"
  26. onMismatch="NEUTRAL" />
  27. <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
  28. <ThresholdFilter level="info" onMatch="ACCEPT"
  29. onMismatch="DENY" />
  30. </Filters>
  31. <Policies>
  32. <TimeBasedTriggeringPolicy />
  33. <SizeBasedTriggeringPolicy size="900MB" />
  34. </Policies>
  35. <DefaultRolloverStrategy max="15" />
  36. </RollingFile>
  37. <RollingFile name="RollingFileWarn"
  38. fileName="${FILE_PATH}/${FILE_NAME}-warn.log"
  39. filePattern="${FILE_PATH}/${FILE_NAME}-warn-%d{yyyy-MM-dd}_%i.log">
  40. <Filters>
  41. <ThresholdFilter level="error" onMatch="DENY"
  42. onMismatch="NEUTRAL" />
  43. <ThresholdFilter level="warn" onMatch="ACCEPT"
  44. onMismatch="DENY" />
  45. </Filters>
  46. <PatternLayout pattern="${LOG_PATTERN}" />
  47. <Policies>
  48. <TimeBasedTriggeringPolicy />
  49. <SizeBasedTriggeringPolicy size="900MB" />
  50. </Policies>
  51. <!-- 最多同一文件夹下30个文件开始覆盖 -->
  52. <DefaultRolloverStrategy max="30" />
  53. </RollingFile>
  54. <RollingFile name="RollingFileError"
  55. fileName="${FILE_PATH}/${FILE_NAME}-error.log"
  56. filePattern="${FILE_PATH}/${FILE_NAME}-error-%d{yyyy-MM-dd}_%i.log">
  57. <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
  58. <ThresholdFilter level="error" onMatch="ACCEPT"
  59. onMismatch="DENY" />
  60. <PatternLayout pattern="${LOG_PATTERN}" />
  61. <Policies>
  62. <TimeBasedTriggeringPolicy />
  63. <SizeBasedTriggeringPolicy size="900MB" />
  64. </Policies>
  65. <DefaultRolloverStrategy max="30" />
  66. </RollingFile>
  67. </appenders>
  68. <loggers>
  69. <logger name="org.mybatis" level="info" additivity="false">
  70. <AppenderRef ref="Console" />
  71. </logger>
  72. <Logger name="org.springframework" level="info"
  73. additivity="false">
  74. <AppenderRef ref="Console" />
  75. </Logger>
  76. <root level="info">
  77. <appender-ref ref="Console" />
  78. <appender-ref ref="Filelog" />
  79. <appender-ref ref="RollingFileInfo" />
  80. <appender-ref ref="RollingFileWarn" />
  81. <appender-ref ref="RollingFileError" />
  82. </root>
  83. </loggers>
  84. </configuration>

全异步配置(启动脚本中增加一个系统变量)

-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector