Spring启动logback fileappender配置

时间:2015-04-14 16:44:50

标签: spring logging logback

我有一个非常简单的Spring boot 1.2.2 Web应用程序,我已经为其创建了一个logback.xml配置文件:

<configuration debug="true" scan="true" scanPeriod="33 seconds">

  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

<!--
  <appender name="logfile" class="ch.qos.logback.core.FileAppender">
    <file>myapp.log</file>
    <append>true</append>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS %-5level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>      
  </appender>
-->

  <logger name="com.myapp.logtest.SampleController" level="DEBUG"/>
  <logger name="com.myapp.logtest" level="WARN">
  </logger>

  <root level="INFO">
    <appender-ref ref="console"/>
  </root>

</configuration>

一切都很棒!以下是logfile的调试输出:

12:29:16,397 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 33 seconds
12:29:16,397 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[...\logtest\logback.xml]] every 33 seconds. 
12:29:16,397 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
12:29:16,397 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
12:29:16,397 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [console]
12:29:16,407 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
12:29:16,427 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.myapp.logtest.SampleController] to DEBUG
12:29:16,427 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.myapp.logtest] to WARN
12:29:16,427 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
12:29:16,427 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[ROOT]
12:29:16,427 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
12:29:16,427 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@25641d39 - Registering current configuration as safe fallback point

现在,这是我的问题。如果我在配置文件中解除文件appender,即使没有对它进行参考,我的整个logback配置也会被丢弃,我的应用程序将返回使用Spring的日志记录默认值。以下是存在文件appender的logback调试输出:

12:32:58,308 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 33 seconds
12:32:58,308 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[...\logtest\logback.xml]] every 33 seconds. 
12:32:58,308 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
12:32:58,308 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
12:32:58,308 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [console]
12:32:58,328 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
12:32:58,338 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.FileAppender]
12:32:58,338 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [logfile]
12:32:58,338 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
12:32:58,348 |-WARN in Logger[org.springframework.boot.logging.LoggingApplicationListener] - No appenders present in context [default] for logger [org.springframework.boot.logging.LoggingApplicationListener].

正如您所看到的,仅仅存在文件appender会导致logback初始化停止,并发出LoggingApplicationListener的警告。 Logback实例化文件appender,所以我相信它在logback.xml中正确指定。但我的logback.xml被忽略,并且日志记录会回退到Spring默认值。

这个警告意味着什么?如何获得一个logback文件appender来使用Spring启动?

1 个答案:

答案 0 :(得分:0)

检查应用程序属性中是否有logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR。如果application.properties中存在这样的内容,则将忽略logback.xml,并且默认为application.properties中的内容。