了解如何使用多个文件追加器在Spring 引导应用程序中创建多个日志文件。了解如何使用翻转策略、归档等配置所有文件追加器,wiihlog4j2和日志配置。
1. 带登录的多个日志文件
以下文件包含 5 个记录器。我们可以根据需要创建更多的记录器。logback.xml
console
– 将输出记录到控制台。包括所有日志语句。applicationLog
– 将输出记录到。包括包的应用程序日志。application.log
debug
com.howtodoinjava.demo
aopLog
– 将输出记录到。在包中包含应用程序日志。application-aop.log
info
com.howtodoinjava.demo.aop
springLog
– Createand 包含 spring 框架生成的所有错误日志。spring-framework.log
hibernateLog
– 创建并包含休眠生成的所有错误日志。database.log
放置给定资源文件夹。
logback.xml
< configuration >
< property name = "LOG_PATTERN" value = "%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n" />
< property name = "APP_LOG_ROOT" value = "c:/temp" />
< appender name = "console" class = "ch.qos.logback.core.ConsoleAppender" >
< encoder >
< pattern >${LOG_PATTERN}</ pattern >
</ encoder >
</ appender >
< appender name = "applicationLog" class = "ch.qos.logback.core.rolling.RollingFileAppender" >
< file >${APP_LOG_ROOT}/application.log</ file >
< encoder >
< pattern >${LOG_PATTERN}</ pattern >
</ encoder >
< rollingPolicy class = "ch.qos.logback.core.rolling.FixedWindowRollingPolicy" >
< fileNamePattern >${APP_LOG_ROOT}/application-%i.log</ fileNamePattern >
< minIndex >1</ minIndex >
< maxIndex >10</ maxIndex >
</ rollingPolicy >
< triggeringPolicy class = "ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
< maxFileSize >10MB</ maxFileSize >
</ triggeringPolicy >
</ appender >
< appender name = "aopLog" class = "ch.qos.logback.core.rolling.RollingFileAppender" >
< file >${APP_LOG_ROOT}/application-aop.log</ file >
< encoder >
< pattern >${LOG_PATTERN}</ pattern >
</ encoder >
< rollingPolicy class = "ch.qos.logback.core.rolling.FixedWindowRollingPolicy" >
< fileNamePattern >${APP_LOG_ROOT}/application-aop-%i.log</ fileNamePattern >
< minIndex >1</ minIndex >
< maxIndex >10</ maxIndex >
</ rollingPolicy >
< triggeringPolicy class = "ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
< maxFileSize >10MB</ maxFileSize >
</ triggeringPolicy >
</ appender >
< appender name = "springLog" class = "ch.qos.logback.core.rolling.RollingFileAppender" >
< file >${APP_LOG_ROOT}/spring-framework.log</ file >
< encoder >
< pattern >${LOG_PATTERN}</ pattern >
</ encoder >
< rollingPolicy class = "ch.qos.logback.core.rolling.FixedWindowRollingPolicy" >
< fileNamePattern >${APP_LOG_ROOT}/spring-framework-%i.log</ fileNamePattern >
< minIndex >1</ minIndex >
< maxIndex >10</ maxIndex >
</ rollingPolicy >
< triggeringPolicy class = "ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
< maxFileSize >10MB</ maxFileSize >
</ triggeringPolicy >
</ appender >
< appender name = "hibernateLog" class = "ch.qos.logback.core.rolling.RollingFileAppender" >
< file >${APP_LOG_ROOT}/database.log</ file >
< encoder >
< pattern >${LOG_PATTERN}</ pattern >
</ encoder >
< rollingPolicy class = "ch.qos.logback.core.rolling.FixedWindowRollingPolicy" >
< fileNamePattern >${APP_LOG_ROOT}/database-%i.log</ fileNamePattern >
< minIndex >1</ minIndex >
< maxIndex >10</ maxIndex >
</ rollingPolicy >
< triggeringPolicy class = "ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
< maxFileSize >10MB</ maxFileSize >
</ triggeringPolicy >
</ appender >
< logger name = "com.howtodoinjava.demo" level = "DEBUG" >
< appender-ref ref = "applicationLog" />
< appender-ref ref = "console" />
</ logger >
< logger name = "com.howtodoinjava.demo.aop" level = "INFO" >
< appender-ref ref = "aopLog" />
< appender-ref ref = "console" />
</ logger >
< logger name = "org.springframework" level = "DEBUG" >
< appender-ref ref = "springLog" />
< appender-ref ref = "console" />
</ logger >
< logger name = "org.hibernate" level = "DEBUG" >
< appender-ref ref = "hibernateLog" />
< appender-ref ref = "console" />
</ logger >
< root level = "info" >
< appender-ref ref = "console" />
</ root >
</ configuration >
|
2. 带有 log4j2 的多个日志文件
要使用 log4j2 实现上述日志记录配置,请在类路径中包含 log4j2 依赖项。
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-web</ artifactId >
< exclusions >
< exclusion >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-logging</ artifactId >
</ exclusion >
</ exclusions >
</ dependency >
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-log4j2</ artifactId >
</ dependency >
|
现在在资源文件夹中添加log4j2.xml。
<? xml version = "1.0" encoding = "UTF-8" ?>
< Configuration status = "WARN" monitorInterval = "30" >
< Properties >
< Property name = "LOG_PATTERN" >%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</ Property >
< Property name = "APP_LOG_ROOT" >c:/temp</ Property >
</ Properties >
< Appenders >
< Console name = "console" target = "SYSTEM_OUT" follow = "true" >
< PatternLayout pattern = "${LOG_PATTERN}" />
</ Console >
< RollingFile name = "applicationLog" fileName = "${sys:APP_LOG_ROOT}/application.log"
filePattern = "${sys:APP_LOG_ROOT}/application-%d{yyyy-MM-dd}-%i.log" >
< PatternLayout pattern = "${LOG_PATTERN}" />
< Policies >
< SizeBasedTriggeringPolicy size = "19500KB" />
</ Policies >
< DefaultRolloverStrategy max = "10" />
</ RollingFile >
< RollingFile name = "springLog" fileName = "${sys:APP_LOG_ROOT}/spring-framework.log"
filePattern = "${sys:APP_LOG_ROOT}/spring-%d{yyyy-MM-dd}-%i.log" >
< PatternLayout pattern = "${LOG_PATTERN}" />
< Policies >
< SizeBasedTriggeringPolicy size = "19500KB" />
</ Policies >
< DefaultRolloverStrategy max = "10" />
</ RollingFile >
< RollingFile name = "aopLog" fileName = "${sys:APP_LOG_ROOT}/application-aop.log"
filePattern = "${sys:APP_LOG_ROOT}/application-aop-%d{yyyy-MM-dd}-%i.log" >
< PatternLayout pattern = "${LOG_PATTERN}" />
< Policies >
< SizeBasedTriggeringPolicy size = "19500KB" />
</ Policies >
< DefaultRolloverStrategy max = "10" />
</ RollingFile >
< RollingFile name = "hibernateLog" fileName = "${sys:APP_LOG_ROOT}/database.log"
filePattern = "${sys:APP_LOG_ROOT}/spring-%d{yyyy-MM-dd}-%i.log" >
< PatternLayout pattern = "${LOG_PATTERN}" />
< Policies >
< SizeBasedTriggeringPolicy size = "19500KB" />
</ Policies >
< DefaultRolloverStrategy max = "10" />
</ RollingFile >
</ Appenders >
< Loggers >
< Logger name = "com.howtodoinjava.demo" additivity = "false" level = "debug" >
< AppenderRef ref = "applicationLog" />
< AppenderRef ref = "console" />
</ Logger >
< Logger name = "com.howtodoinjava.demo.aop" additivity = "false" level = "info" >
< AppenderRef ref = "aopLog" />
< AppenderRef ref = "console" />
</ Logger >
< Logger name = "org.springframework" additivity = "false" level = "error" >
< AppenderRef ref = "springLog" />
< AppenderRef ref = "console" />
</ Logger >
< Logger name = "org.hibernate" additivity = "false" level = "error" >
< AppenderRef ref = "hibernateLog" />
< AppenderRef ref = "console" />
</ Logger >
< Root level = "INFO" >
< AppenderRef ref = "console" />
</ Root >
</ Loggers >
</ Configuration >
|
3. 弹簧启动多个日志文件演示
使用上述任何给定配置运行应用程序。您将看到文件夹中生成的日志文件。'c:/temp'
Spring 引导多个日志文件示例
请向我提出与使用多个文件追加器配置 Spring 引导日志记录相关的问题。
快乐学习!!