Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure logback conf to send all messages to stderr?

Recently added some println output to my program... and now dont want logback INFO messages merging with them. How can I get all messages to stderr ?

like image 943
Alan Jurgensen Avatar asked Sep 19 '14 13:09

Alan Jurgensen


People also ask

How do I set debug level in Logback?

debug=true to enable debugging of the logback setup. Unfortunately, there is no way to enable debugging via a System property. You have to use <configuration debug="true"> in the logback. xml .

What is Appender in Logback?

Appenders place log messages in their final destinations. A Logger can have more than one Appender. We generally think of Appenders as being attached to text files, but Logback is much more potent than that. Layout prepares messages for outputting.

What is Logback configuration?

Logback executes an async appender in a separate thread to decouple the logging overhead from the thread executing your code. Using the async appender is incredibly easy. Refer the appender that should be asynchronously invoked within an <appender> element.


2 Answers

Create appender and attach all logs for it:

<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
    <target>System.err</target>
    <encoder>
        <pattern>%date [%thread] - 5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<logger name="org.apache" level="INFO ">
    <appender-ref ref="STDERR"/>
</logger>

All org.apache class logs(that uses slf4j) will be directed to System.err

like image 181
Elbek Avatar answered Oct 20 '22 11:10

Elbek


Use a ConsoleAppender with the target attribute set to System.err. See http://logback.qos.ch/manual/appenders.html#ConsoleAppender for details.

Something like this in your logback.xml should work:

<configuration>
  <appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <target>System.err</target>
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="STDERR" />
  </root>
</configuration>
like image 35
Richard Neish Avatar answered Oct 20 '22 11:10

Richard Neish