Spring boot 1.5.2.RELEASE:Rolling File logging

时间:2017-06-08 02:54:24

标签: java spring logging spring-boot

我在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>

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

1 个答案:

答案 0 :(得分:0)

您的logback.xml位于资源文件夹中的哪个位置?如果是这样,这应该立即工作(只测试你的配置,它的工作原理)。如果没有,您必须指定它的位置。例如。如果它位于...resources/config下,则必须将以下条目添加到application.yml

logging:
  config: classpath:config/logback.xml