April 3, 2016

Logback and Spring Boot integration

If you want to use Logback as a logging tool for your Spring Boot project, you have to set the path for your logback.xml settings in the application.properties file. Note that it is for Spring Boot in the 1.3.3 version (and later I guess). The important thing is the colon ":"

In "application.properties" file:


#Logback config file

logging.config: ${user.dir}/config/logback.xml



and the logback.xml:
<configuration debug="false">

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->

    <encoder>

      <pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{40} - %msg%n</pattern>

    </encoder>

  </appender>


  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

    <file>log/project.log</file>

    <append>true</append>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

      <!-- rollover daily -->

      <fileNamePattern>log/project-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>

      <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

        <!-- or whenever the file size reaches 10MB -->

        <maxFileSize>40MB</maxFileSize>

      </timeBasedFileNamingAndTriggeringPolicy> 

      <!-- keep 120 days' worth of history -->

      <maxHistory>120</maxHistory>

    </rollingPolicy>

    <encoder>

      <pattern>%d{HH:mm:ss.SSS} [%-5level] %-40logger - %msg%n</pattern>

    </encoder>

  </appender>


  <root level="WARN">

    <appender-ref ref="STDOUT" />

    <appender-ref ref="FILE" />

  </root>

</configuration>

No comments:

Post a Comment