Charlie Davis
Charlie Davis

Reputation: 51

Spring boot logback fileappender configuration

I have a very simple Spring boot 1.2.2 web application for which I have created a logback.xml configuration file:

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

Everything works great! Here is the debug output from 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

Now, here is my problem. If I UNCOMMENT the file appender in the configuration file, even without making a REFERENCE to it, my entire logback configuration is discarded and my application goes back to using the Spring logging defaults. Here is the logback debug output with the file appender present:

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].

As you can see, the mere presence of the file appender causes logback initialization to cease with a warning from LoggingApplicationListener. Logback instantiates the file appender, so I believe that it is correctly specified in logback.xml. But my logback.xml is ignored, and logging falls back to Spring defaults.

What does this warning mean? How can I get a logback file appender to work with Spring boot?

Upvotes: 0

Views: 3692

Answers (1)

chetank
chetank

Reputation: 402

Check if you have the logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR in the pplication properties. if anything like this is present in application.properties your logback.xml will be ignored and will default to what is present in application.properties.

Upvotes: 0

Related Questions