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.