Search

Dark theme | Light theme
Showing posts with label Logging. Show all posts
Showing posts with label Logging. Show all posts

August 26, 2015

Show Logback Configuration Status with Groovy Configuration

Logback is a SLF4J API implementation for logging messages in Java and Groovy. We can configure Logback with a Groovy configuration file. The file is a Groovy script and allows for a nice an clean way (no XML) to configure Logback. If we want to show the logging configuration and see how Logback is configured we must add a StatusListener implementation in our configuration. The StatusListener implementation prints out the configuration when our application starts. Logback provides a StatusListener for outputting the information to system out or system error streams (OnConsoleStatusListener and OnErrorConsoleStatusListener). If we want to disable any status messages we use the NopStatusListener class.

In the following example configuration file we define the status listener with the statusListener method. We use the OnConsoleStatusListener in our example:

// Add status listener, which prints out
// the Logback configuration on application
// startup.
statusListener(OnConsoleStatusListener)

appender('SystemOut', ConsoleAppender) {
    // Enable coloured output.
    withJansi = true

    encoder(PatternLayoutEncoder) {
        pattern = "%blue(%-5level) %green(%logger{35}) - %msg %n"
    }
}

root(DEBUG, ['SystemOut'])

If we run our Java or Groovy application with this configuration we get the following information in our console:

    09:31:09,143 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@3b0f5da3 - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener]
    09:31:09,167 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@3b0f5da3 - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
    09:31:09,168 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@3b0f5da3 - Naming appender as [SystemOut]
    09:31:09,239 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@3b0f5da3 - Setting level of logger [ROOT] to DEBUG
    09:31:09,244 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@3b0f5da3 - Attaching appender named [SystemOut] to Logger[ROOT]

Alternatively we can use the Java system property logback.statusListenerClass when we run our application. We must provide the full class name for the StatusListener implementation. This will overrule any status listeners that are defined in configuration files.

Written with Logback 1.1.3.

August 24, 2015

Redirect Logging Output to Standard Error with Logback

When we use Logback as SLF4J API implementation in our code we can have our logging output send to our console. By default the standard output is used to display the logging output. We can alter the configuration and have the logging output for the console send to standard error. This can be useful when we use a framework that uses the standard output for communication and we still want to see logging from our application on the console. We set the property target for the ConsoleAppender to the value System.err instead of the default System.out.

The following sample Logback configuration in Groovy send the logging output to the console and standard error:

appender("SystemErr", ConsoleAppender) {
    // Enable coloured output.
    withJansi = true

    encoder(PatternLayoutEncoder) {
        pattern = "%blue(%-5level) %green(%logger{35}) - %msg %n"
    }

    // Redirect output to the System.err.
    target = 'System.err'

}

root(DEBUG, ["SystemErr"])

If we want to use the XML format we get the following configuration:

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

    <appender name="SystemErr"
              class="ch.qos.logback.core.ConsoleAppender">
        
        <target>System.err</target>
        <withJansi>true</withJansi>

        <encoder>
            <pattern>%blue(%-5level) %green(%logger{35}) - %msg %n</pattern>
        </encoder>

    </appender>

    <root level="DEBUG">
        <appender-ref ref="SystemErr"/>
    </root>

</configuration>

Written with Logback 1.1.3.