Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Redirect Logging Output to Standard Error with Logback

DZone's Guide to

Redirect Logging Output to Standard Error with Logback

Learn how to redirect a logging output to a standard error in Logback.

Free Resource

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

When we use Logback as a SLF4J API implementation in our code we can have our logging output sent     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"?>
01.
<configuration>
02.

03.
<appender name="SystemErr"
04.
class="ch.qos.logback.core.ConsoleAppender"
05.
target="System.err"
06.
withJansi="true">
07.

08.
<encoder>
09.
<pattern>%blue(%-5level) %green(%logger{35}) - %msg %n</pattern>
10.
</encoder>
11.

12.
</appender>
13.

14.
<root level="DEBUG">
15.
<appender-ref ref="SystemErr"/>
16.
</root>
17.

18.
</configuration>


Learn tips and best practices for optimizing your capacity management strategy with the Market Guide for Capacity Management, brought to you in partnership with BMC.

Topics:
error log ,slf4j ,logback ,logging ,performance

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}