svjn
svjn

Reputation: 914

Spring Boot with Logback + springProperty

I have configured my logback-spring.xml as per Spring Boot Documentation. Here is my logback-spring.xml file

<configuration>

    <springProperty name="appName" source="spring.application.name" defaultValue="myLogFile" />

    <property name="log.date" value="%d{yyyy-MM-dd}" />
    <property name="log.path" value="/log" />
    <property name="log.file" value="${appName}" />
    <property name="log.live.path" value="${log.path}/${log.file}.log" />

    <appender name="myLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.live.path}</file>
        <append>true</append>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/archive/${log.file}.${log.date}.log.gz</fileNamePattern>
            <maxHistory>1</maxHistory>
        </rollingPolicy>
    </appender>

    <logger name="com.log.logback" level="INFO" />
    <logger name="com.log.sample" level="INFO" />

    <root level="ERROR">
        <appender-ref ref="myLogAppender" />
    </root>
</configuration>

application.properties

spring.application.name=mybootapp

And while I start my spring boot application, I see 2 directories created under /log path which are myLogFile and bootstrap. From the logs, I could see bootstrap directory is created before logback could access the spring property. I have no clue why bootstrap directory is created. I didn't see this issue before using <springProperty> in logback xml. Is there any configuration that I am missing here? I couldn't find any relevant info online.

Logback logs for the reference.

15:54:02,085 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@610f7aa - URL [jar:file:/C:/projects/jars/logback-logging-17.2.2.INT.jar!/logback-spring.xml] is not of type file
15:54:02,132 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [jar:file:/C:/projects/jars/logback-logging-17.2.2.INT.jar!/logback-spring.xml] 
15:54:02,132 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 5 minutes
15:54:02,132 |-WARN in Logger[org.springframework.core.env.PropertySourcesPropertyResolver] - No appenders present in context [default] for logger [org.springframework.core.env.PropertySourcesPropertyResolver].
15:54:02,274 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:54:02,274 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [myLogAppender]
15:54:02,305 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@867988177 - Will use gz compression
15:54:02,305 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@867988177 - Will use the pattern /log/archive/bootstrap.%d{yyyy-MM-dd}.log for the active file
15:54:02,305 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern '/log/archive/bootstrap.%d{yyyy-MM-dd}.log.gz'.
15:54:02,305 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
15:54:02,305 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Tue Apr 18 15:45:05 EDT 2017
15:54:02,320 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[myLogAppender] - Active log file name: /log/bootstrap.log
15:54:02,320 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[myLogAppender] - File property is set to [/log/bootstrap.log]
15:54:02,336 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.log.logback] to INFO
15:54:02,336 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@48e1f6c7 - Propagating INFO level on Logger[com.log.logback] onto the JUL framework
15:54:02,336 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.log.sample] to INFO
15:54:02,336 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@48e1f6c7 - Propagating INFO level on Logger[com.log.sample] onto the JUL framework
15:54:02,336 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to ERROR
15:54:02,336 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@48e1f6c7 - Propagating ERROR level on Logger[ROOT] onto the JUL framework
15:54:02,336 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [myLogAppender] to Logger[ROOT]
15:54:02,336 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
15:54:02,336 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@26abb146 - Registering current configuration as safe fallback point
15:54:03,139 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@242aa8d9 - URL [jar:file:/C:/projects/jars/logback-logging-17.2.2.INT.jar!/logback-spring.xml] is not of type file
15:54:03,139 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [jar:file:/C:/projects/jars/logback-logging-17.2.2.INT.jar!/logback-spring.xml] 
15:54:03,139 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 5 minutes
15:54:03,139 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
15:54:03,139 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [myLogAppender]
15:54:03,139 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@300604602 - Will use gz compression
15:54:03,139 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@300604602 - Will use the pattern /log/archive/mybootapp.%d{yyyy-MM-dd}.log for the active file
15:54:03,140 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern '/log/archive/mybootapp.%d{yyyy-MM-dd}.log.gz'.
15:54:03,140 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
15:54:03,140 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Tue Apr 18 15:53:03 EDT 2017
15:54:03,140 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[myLogAppender] - Active log file name: /log/mybootapp.log
15:54:03,140 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[myLogAppender] - File property is set to [/log/mybootapp.log]
15:54:03,140 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.log.logback] to INFO
15:54:03,140 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@1b065145 - Propagating INFO level on Logger[com.log.logback] onto the JUL framework
15:54:03,140 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.log.sample] to INFO
15:54:03,140 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@1b065145 - Propagating INFO level on Logger[com.log.sample] onto the JUL framework
15:54:03,140 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to ERROR
15:54:03,140 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@1b065145 - Propagating ERROR level on Logger[ROOT] onto the JUL framework
15:54:03,140 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [myLogAppender] to Logger[ROOT]
15:54:03,140 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
15:54:03,140 |-INFO in org.springframework.boot.logging.logback.SpringBootJoranConfigurator@45cff11c - Registering current configuration as safe fallback point

Can someone pls help.

Upvotes: 4

Views: 22722

Answers (2)

Archimedes Trajano
Archimedes Trajano

Reputation: 41790

It may be because I updated to 1.5.18 just now but this appears to work, my only change from yours is the addition of scope="context". Here's how I did it with the GELF appender

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
  <include resource="org/springframework/boot/logging/logback/base.xml"/>
  <springProperty scope="context" name="name" source="spring.application.name"/>

  <appender name="gelf" class="de.appelgriepsch.logback.GelfAppender">
    <server>192.168.1.113</server>
    <includeSource>true</includeSource>
    <includeMDC>true</includeMDC>
    <includeLevelName>true</includeLevelName>
    <additionalFields>name=${name}</additionalFields>
  </appender>
  <root>
    <appender-ref ref="gelf"/>
  </root>
</configuration>

Upvotes: 6

xiong
xiong

Reputation: 25

If you’re using a custom logback-spring.xml then you have to pass the spring.application.name in bootstrap instead of application property file. Otherwise your custom logback file won’t read the property properly.

you need to set up spring.application.name in bootstrap.yml

see: http://cloud.spring.io/spring-cloud-sleuth/spring-cloud-sleuth.html

Upvotes: 0

Related Questions