Spring Boot Console Logging

Learn to use and configure logging to the console in a Spring boot application. Also, learn to configure and use console appenders available in logback and log4j2.

1. Default Logging in Spring Boot

Spring boot internally uses Apache’s common logging and uses Logback as the default logging provider. If we do not make any logging-specific configuration, we still see lots of logging output to the console which is good enough for POC (proof of concept) purposes.

We can find all default console logging configurations in these packaged files.

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

<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-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}}"/>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>${CONSOLE_LOG_PATTERN}</pattern>
    </encoder>
</appender>

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

2. Customize the Console Logs via Properties/YAML

Generally, in the console logs, we would want to do two things i.e. log levels and output pattern.

2.1. Log Levels

To change the log level, we can change it at two levels – root logger and/or specific logger instance.

To change the level at the root logger, ‘logging.level.root=trace‘ (with the desired level) in the properties file. Additionally, to control logging at individual logger level, use logging.level.LOGGER_NAME=trace.

logging.level.root=INFO

logging.level.org.springframework=ERROR
logging.level.org.hibernate=ERROR

2.2. Log Output Pattern

The pattern is controlled vis property logging.pattern.console and logging.pattern.file properties.

logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n

logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n

2.3. Log Thresholds

If we are using Logback or Log4j2, we can configure different log levels for console logs and file logs using the configuration properties logging.threshold.console and logging.threshold.file.

logging.threshold.console=TRACE

logging.threshold.file=INFO

3. Custom Console Logging using logback.xml

If we have configured logging via logback – using own logback.xml file then use the below configuration to control various options of console logging.

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

    <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} – %logger{36} – %msg%n" />

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <logger name="com.howtodoinjava.app" level="trace" additivity="false">
        <appender-ref ref="console" />
    </logger>

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

</configuration>

4. Custom Console Logging using log4j2.xml

To configure console logging with log4j2, place the given definition in log4j2.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">

    <Properties>
        <Property name="LOG_PATTERN">%d{yyyy-MM-dd’T’HH:mm:ss.SSSZ} %p %m%n</Property>
    </Properties>

    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="${LOG_PATTERN}" />
        </Console>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="console" />
        </Root>
    </Loggers>
</Configuration>

5. Disable the Console Logging

To disable logging in the console, the cleanest way is to create a custom logging configuration file (logback.xml or log4j2.xml) and not provide any definition of a console appender.

Only include file appenders in the configuration file which will prevent any logging output to the console.

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

    <property name="LOG_LOCATION" value="c:/temp" />
    <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} – %logger{36} – %msg%n" />

    <appender name="file" class="ch.qos.logback.core.FileAppender">
        <file>${LOG_LOCATION}/mylog.log</file>
        <encoder>
        <pattern>${LOG_PATTERN}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_LOCATION}/archived/mylog-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <logger name="com.howtodoinjava.app" level="trace" additivity="false">
        <appender-ref ref="file" />
    </logger>

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

</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">

    <Properties>
        <Property name="LOG_PATTERN">%d{yyyy-MM-dd’T’HH:mm:ss.SSSZ} %p %m%n</Property>
    </Properties>

    <RollingFile name="file"
        fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log"
        filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log">
        <PatternLayout pattern="${LOG_PATTERN}" />
        <Policies>
            <SizeBasedTriggeringPolicy size="19500KB" />
        </Policies>
        <DefaultRolloverStrategy max="1" />
    </RollingFile>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="file" />
        </Root>
    </Loggers>
</Configuration>

If not using configuration files, we can use the property logging.pattern.console with empty value to disable console logging. It is not the recommended approach and do it only for POCs.

logging.pattern.console=

Drop me your questions related to Spring boot console appender example.

Happy Learning !!

Comments

Subscribe
Notify of
guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode