Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot logging pattern

I have a problem with configuration on Logback in a Spring Boot application. I want my consoleAppender to look like the default Spring Boot console appender. How to inherit pattern from Spring Boot default console appender?

Below is my consoleAppender configuration

<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern class="org.">
            %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
        </Pattern>
    </layout>
</appender>
like image 278
Kacper Avatar asked Jun 01 '15 10:06

Kacper


People also ask

What is the spring boot default logging pattern?

The default logging configuration in Spring Boot is a Logback implementation at the info level for logging the output to console. The first info log is printed, followed by a seven-line banner of Spring and then the next info log. The debug statement is suppressed.

Which logging framework is best for spring boot?

Spring Boot's default logging framework is Logback. Your application code should interface only with the SLF4J facade so that it's easy to switch to an alternative framework if necessary. Log4j2 is newer and claims to improve on the performance of Logback.

How does Spring Boot configure logging?

Spring Boot has a LoggingSystem abstraction that attempts to configure logging based on the content of the classpath. If Logback is available, it is the first choice. You can also set the location of a file to which to write the log (in addition to the console) by using "logging. file".

How do I write logs in spring boot?

To make Spring Boot write its log to disk, set the path and filename. With this configuration, Spring Boot will write to the console and also to a log file called spring. log , at the path you specify.


3 Answers

Once you have included the default configuration, you can use its values in your own logback-spring.xml configuration:

<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true">     <!-- use Spring default values -->     <include resource="org/springframework/boot/logging/logback/defaults.xml"/>      <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">         <encoder>             <pattern>${CONSOLE_LOG_PATTERN}</pattern>             <charset>utf8</charset>         </encoder>     </appender>     … </configuration> 
like image 94
Michael Piefel Avatar answered Sep 28 '22 10:09

Michael Piefel


You can find Spring Boot logback console logging pattern in defaults.xml file:

spring-boot-1.5.0.RELEASE.jar/org/springframework/boot/logging/logback/defaults.xml

Console pattern:

<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> 
like image 25
lapaczo Avatar answered Sep 28 '22 11:09

lapaczo


<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
            </Pattern>
        </layout>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>
like image 20
Lukasz Frankowski Avatar answered Sep 28 '22 11:09

Lukasz Frankowski