我在Spring Boot应用程序中使用Logback进行日志管理,我需要这些规范:
答案 0 :(得分:1)
我使用了这个logback.xml配置文件,它运行得很好:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<property name="DEV_HOME" value="logging"/>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover. Make sure the path matches the one in the file element or else
the rollover logs are placed in the working directory. -->
<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE"/>
</root>
<root level="INFO">
<appender-ref ref="consoleAppender" />
</root>
</configuration>
答案 1 :(得分:0)
如果您不打算使用spring扩展进行回退,那么您需要做的就是定义logback.xml
(或logback.groovy
)并将其放入classpath
。
回滚追加器可以定义为:
appender("FILE", RollingFileAppender) {
file = "root.log"
encoder(PatternLayoutEncoder) {
pattern = "%date %level [%thread] %logger{10} [%file:%line] %msg%n"
}
rollingPolicy(TimeBasedRollingPolicy) {
fileNamePattern = "root-%d{yyyy-MM-dd}.log"
maxHistory = 7
}
}
这是groovy,它是一个基于时间的回滚追加器。您可以谷歌搜索更多示例。
如果您打算使用spring扩展程序,请参阅here。
答案 2 :(得分:0)
此替代方案包含 Redis appender:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<property name="DEV_HOME" value="logs"/>
<!--appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashAccessTcpSocketAppender">
<destination>127.0.0.1:4560</destination>
<waitStrategyType>sleeping</waitStrategyType>
<includeCallerInfo>true</includeCallerInfo>
<encoder class="net.logstash.logback.encoder.LogstashAccessEncoder" >
<timeZone>UTC+1</timeZone>
</encoder>
</appender-->
<appender name="REDIS" class="com.cwbase.logback.RedisAppender">
<host>localhost</host>
<port>6379</port>
<key>fouras-logs</key>
<type>fouras-log</type>
<database>12</database>
<sourceHost>localhost</sourceHost>
<source>fouras</source>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<Pattern>[%d{yyyy-MM-dd HH:mm:ss:SSS} %thread %-5level %logger{1000} - %msg]%n</Pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover. Make sure the path matches the one in the file element or else
the rollover logs are placed in the working directory. -->
<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS} %thread %-5level %logger{1000} - %msg]%n</pattern>
</encoder>
</appender>
<logger name="com.datcom.fouras" level="TRACE">
<appender-ref ref="REDIS"/>
<!--appender-ref ref="LOGSTASH"/-->
</logger>
<root level="DEBUG">
<appender-ref ref="FILE"/>
</root>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<!--appender-ref ref="REDIS"/-->
</root>
</configuration>