Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

I have been using Logback for a while. It’s pretty easy to use and it’s fairly easy to configure. I noticed that I had some warning messages in my log file.

10:51:04,532 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
10:51:04,532 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
10:51:04,532 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/Users/norrisshelton/IdeaProjects/gsi-adapter/gsi-adapter-services/target/gsi-services/WEB-INF/classes/logback.xml]
10:51:04,578 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
10:51:04,586 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[/Users/norrisshelton/IdeaProjects/gsi-adapter/gsi-adapter-services/target/gsi-services/WEB-INF/classes/logback.xml]] every 60 seconds. 
10:51:04,586 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
10:51:04,589 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
10:51:04,593 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
10:51:04,649 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - This appender no longer admits a layout as a sub-component, set an encoder instead.
10:51:04,649 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
10:51:04,649 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
10:51:04,650 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.cdi] to DEBUG
10:51:04,650 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.springframework.web.client.RestTemplate] to DEBUG
10:51:04,650 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to WARN
10:51:04,650 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
10:51:04,651 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
10:51:04,652 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@c28ccfa - Registering current configuration as safe fallback point

I checked and discovered that the logging configuration had changed. An example is it used to look like:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS}|%-5level|${HOSTNAME}|cdi-services|%msg ||%class:%line %xException{full} %n</Pattern>
        </layout>
    </appender>

    <logger name="com.cdi" level="DEBUG"/>
    <!-- Show info on rest calls -->
    <logger name="org.springframework.web.client.RestTemplate" level="DEBUG"/>

    <root level="WARN">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

I was getting messages about layout should be wrapped in an encoder. After some tinkering, I arrived at the following:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
    <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|${HOSTNAME}|cdi-services|%msg ||%class:%line %xException{full} %n</pattern>
        </encoder>
    </appender>

    <logger name="com.cdi" level="DEBUG"/>
    <!-- Show info on rest calls -->
    <logger name="org.springframework.web.client.RestTemplate" level="DEBUG"/>

    <root level="WARN">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

The gist of it is that the appender now has an encoder instead of a layout. When the correct changes are made to the configuration file, there is no longer any start-up logging from LogBack in the logs, but you will see that the new logging format is being honored.

A little note is that I leave a space after msg% to help with the wrapping when the lines are too long.

|%msg |

August 11th, 2015

Posted In: Java, java ninja, Javaninja, logback, Logging, Logging configuration

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By : XYZScripts.com