69

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>
Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
Kacper
  • 1,000
  • 2
  • 10
  • 18
  • Maybe you should follow issue https://github.com/spring-projects/spring-boot/issues/1788 - this may give you a solution when closed. – Bertrand Renuart Jun 02 '15 at 21:54

11 Answers11

114

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>
Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
46

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}}"/>
lapaczo
  • 746
  • 7
  • 10
  • 1
    what does LOG_EXCEPTION_CONVERSION_WORD do? – Kalpesh Soni Oct 09 '18 at 17:06
  • This pattern goes through some variable substitution resulting in a different pattern – cdalxndr Feb 22 '20 at 14:20
  • 1
    https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml – Jason Law Sep 01 '21 at 07:03
  • ^^ I had to replace `:-` with `:` when I copied the latest log pattern from above Spring defaults.xml in order for it to work in my application.properties file (as `logging.pattern.console` and `logging.pattern.file`). `:-` is a Logback thing and `:` is a Spring thing. – Alexander Taylor Nov 02 '22 at 02:08
15
<?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>
Lukasz Frankowski
  • 2,955
  • 1
  • 31
  • 32
  • 1
    are those conversion rules supposed to bring spring-defined colouring to logback? It's not working for me – jediz Oct 01 '19 at 19:34
15

If you are using application.yml for your config, you can set the logging pattern this way:

logging:
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss} | %-5level | %logger{1.} | %msg%n"
  level:
    org.springframework: WARN
    com.ulisesbocchio.jasyptspringboot: WARN
    com.example.test: DEBUG

You can override the logging level on the command line. For example:

$ java -Dlogging.level.com.example.test=TRACE -jar my-example.jar
Joman68
  • 2,248
  • 3
  • 34
  • 36
  • 1
    Thank you, I needed to update this without creating a whole logback config file so this is exactly what I was looking for. – Sherwin F Nov 04 '22 at 01:19
9

It's been some time since this question was asked but since I had the problem myself recently and couldn't find an answer I started digging a bit deeper and found a solution that worked for me.

I ended up using the debugger and take a look at the default appenders attached to the logger.

I found this pattern to be working as desired for me:

<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p 18737 --- [%t] %-40.40logger{39} : %m%n%wEx</pattern>

EDIT: The pattern is not entirely correct, I saw that runtime some values had already been instantiated (in this case 18737 ---) i will look into the proper variable to substitute there. It does contain the format for fixed length columns though

EDIT 2: Ok, I took another look at the debugger contents. This you can also do yourself by looking at the contents of a logger instance: Debugger(eclipse) Logger Contents

So I ended up using the pattern used in the consoleAppender:

%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(18971){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx

As can be seen here:

Debugger: detailed contents of the encoder pattern

wypieprz
  • 7,981
  • 4
  • 43
  • 46
Edvaaart
  • 113
  • 2
  • 10
  • Thanks, in other answer I added full working logback.xml with this pattern. It requires conversionRules elements from spring default.xml to work. – Lukasz Frankowski Dec 14 '17 at 06:56
  • 1
    Why do you have the value `18971` explicitly in the pattern? Isn't this a meaningful variable which changes between different launches? – Snackoverflow Feb 09 '19 at 12:38
8

Logging pattern can be configured using application.properties file

Example :

# Logging pattern for the console
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
DV Singh
  • 1,038
  • 11
  • 16
2

You can use below pattern :

%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%15.15t] %-40.40logger{1.} : %m%n%wEx
EzLo
  • 13,780
  • 10
  • 33
  • 38
shrey
  • 33
  • 4
1

Note that you can also customize the imported properties.

But beware that at least with spring boot 1.4.3 if you want to customize the properties imported from the defaults.xml, then the customization should be placed BEFORE the include.

For example this customizes the priority to 100 character wide:

<configuration scan="true">
    <property name="LOG_LEVEL_PATTERN" value="%100p" />

    <!-- use Spring default values -->
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
        </layout>
    </appender>

    <logger name="hu" level="debug" additivity="false">
        <appender-ref ref="CONSOLE" />
    </logger>

    <root level="warn">
        <appender-ref ref="CONSOLE" />
    </root>

</configuration>

But this is NOT:

<configuration scan="true">
    <!-- use Spring default values -->
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <property name="LOG_LEVEL_PATTERN" value="%100p" />

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
        </layout>
    </appender>

    <logger name="hu" level="debug" additivity="false">
        <appender-ref ref="CONSOLE" />
    </logger>

    <root level="warn">
        <appender-ref ref="CONSOLE" />
    </root>

</configuration>
riskop
  • 1,693
  • 1
  • 16
  • 34
0

For those who'd like to use Łukasz Frankowski's answer (which looks like the cleanest solution here), but in a groovy version, the "problematic" {$PID:- } part can be expanded like in the following:

logback-spring.groovy

import ch.qos.logback.classic.PatternLayout
import ch.qos.logback.core.ConsoleAppender
import org.springframework.boot.logging.logback.ColorConverter
import org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter
import org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter

import static ch.qos.logback.classic.Level.INFO

conversionRule("clr", ColorConverter)
conversionRule("wex", WhitespaceThrowableProxyConverter)
conversionRule("wEx", ExtendedWhitespaceThrowableProxyConverter)

appender("STDOUT", ConsoleAppender) {
    layout(PatternLayout) {
        def PID = System.getProperty("PID") ?: ''
        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"
    }

}
root(INFO, ["STDOUT"])
0

This worked for me, adding following line to resources/log4j2.properties file

appender.console.layout.pattern = %d{ISO8601} - info: %msg%n ( your custom pattern goes here )
Nishan B
  • 627
  • 7
  • 11
0

The spring documentation has an example of the logback.xml that defines the default.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    </root>
    <logger name="org.springframework.web" level="DEBUG"/>
</configuration>
Mark
  • 1,337
  • 23
  • 34