我试图将每个日期的spring-boot应用程序日志与当前日期,月份和年份保存在不同的目录中。
我尝试使用时间戳,仅当我们每天重新启动tomcat服务器时,它才会将日志存储在单独的目录中。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<logger name="org.hibernate" level="ERROR" />
<logger name="org.mongodb" level="ERROR" />
<property name="DIRECTORY" value="/home/Properties/logs" />
<timestamp key="year" datePattern="yyyy" />
<timestamp key="month" datePattern="MM" />
<timestamp key="date" datePattern="dd" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.sss}][%-5p][%C][%t][%L]%m%n
</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${DIRECTORY}/${year}/${month}/${date}/logfile.log</file>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.sss}][%-5p][%C][%t][%L]%m%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
我希望每个日期的日志都存储在每天创建的目录中,而无需像[year] \ [month] \ [date] \ logfile.log那样重新启动服务器
答案 0 :(得分:0)
结合使用RollingFileAppender
和TimeBasedRollingPolicy
,并将fileNamePattern
设置为%d{yyyy/MM/dd}/logFile.log
。有关更多详细信息,请参见this link
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>%d{yyyy/MM/dd}/logFile.log</fileNamePattern>
<!-- keep 30 days' worth of history capped at 3GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
答案 1 :(得分:0)
找到了我自己的问题的解决方案,如果任何人找到更好的解决方案,请告诉我。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DIRECTORY" value="/home/Properties/logs" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.sss}][%-5p][%C][%t][%L]%m%n
</pattern>
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DIRECTORY}/logfile.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DIRECTORY}/%d{yyyy/MM/dd, aux}/logfile-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
</rollingPolicy>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.sss}][%-5p][%C][%t][%L]%m%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>