gpt4 book ai didi

java - Spring 启动1.5.2.RELEASE : Rolling File logging

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:27 24 4
gpt4 key购买 nike

我在application.yml中有以下配置

logging:
path: logs/
file: logs/service.log
pattern:
console: "%d %-5level %logger : %msg%n"
file: "%d %-5level [%thread] %logger : %msg%n"
level:
com.demo.api: INFO

下面是我的logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
<logger name="com.demo" additivity="false" level="DEBUG">
<appender-ref ref="CONSOLE"/>
</logger>
</configuration>

当我运行 Spring Boot 应用程序时,它没有记录到文件,但我在控制台中看到以下记录,如下所示

2017-06-07 16:14:45.841  INFO 37084 --- [nio-8443-exec-8] c.d.api.Controller        : test msg 1
2017-06-07 16:14:45.854 INFO 37084 --- [nio-8443-exec-8] c.d.api.dao.Dao : test msg 2
2017-06-07 16:14:49.188 INFO 37084 --- [nio-8443-exec-8] c.d.api.service.DigSigService : test msg 3
2017-06-07 16:14:49.199 INFO 37084 --- [nio-8443-exec-8] c.d.api.service.DigSigService : test msg 4
2017-06-07 16:14:49.200 INFO 37084 --- [nio-8443-exec-8] c.d.api.dao.Dao : test msg 5

我尝试更新 logback.xml 如下,但仍然没有锁定记录到文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<property name="DEV_HOME" value="c:/logs" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>

<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>

</appender>

<appender name="FILE-ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/error.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/archived/error.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>

</appender>

<!-- Send logs to both console and file audit -->
<logger name="com.demo.api" level="debug"
additivity="false">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</logger>

<root level="error">
<appender-ref ref="FILE-ERROR" />
</root>

</configuration>

有人可以帮我修复滚动文件的日志记录吗?

最佳答案

您的 logback.xml 位于何处(直接位于资源文件夹中)?如果是这样,这应该立即起作用(刚刚测试了您的配置,它起作用了)。如果没有,您必须指定它所在的位置。例如。如果它位于 ...resources/config 下,则必须将以下条目添加到 application.yml 中:

logging:
config: classpath:config/logback.xml

关于java - Spring 启动1.5.2.RELEASE : Rolling File logging,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44425881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com