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

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 ?

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

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>

Related

logback-classic does not log Java Exceptions

I can confirm that my logback.xml file (placed in src/main/resources) is correctly picked up, and used by the Java executable. I configured it to log everything at INFO level or higher to a FILE and to STDOUT. Everything works as expected (logging is output to both places), except that Exceptions are only logged to STDOUT, but not to the FILE. What's missing in my logback.xml?
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>./logs/batch.log</file>
<append>true</append>
<encoder>
<pattern>%date [%level] from %logger - %message%n%xException</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%level] from %logger - %message%n%xException</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</configuration>

How to customize logback.xml file

I want to customize my logback.xml file for 3 levels (error,debug,info) .I do like this :
<configuration>
<appender name="FILE-ERROR" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE-DEBUG" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE-INFO" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE-ERROR" />
</root>
<root level="DEBUG">
<appender-ref ref="FILE-DEBUG" />
</root>
<root level="ERROR">
<appender-ref ref="FILE-INFO" />
</root>
</configuration>
But I didn't get the correct loges information .Do logback.xml in wrong format?
If I understood correctly, you want to log the Levels ERROR, INFO and DEBUG to one file.
Please change your config as so:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/ApplicationLogs.log</file>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
Logback will then log all events of levels DEBUG and higher. That means it will log DEBUG, INFO, WARN, ERROR.
You can leave out <append>true</append> it is the default.

Can we log mapreduce programs using logback.xml?

Is it possible to log a mapreduce program using slf4j logback.xml ? I have tried logging a mapreduce program using logback.xml.But nothing had logged into console.
My logback.xml:
===============
<configuration>
<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} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Can anyone suggest me on this issue ...
Thanks,in advance.

Logback's debug output not shown

I'm running a Java servlet inside Tomcat 7.0.52.
This is my logback.xml file
<configuration debug="true">
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/home/my-user/my-log.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
Unfortunately I'm nowhere seeing Logback's debug output. Neither in catalina.out, nor in my-log.log or on the console. Where is it?
Just to add: Changes in logback.xml are reflected in the logging output.
In your logback configuration, you define a file appender. What you do not do is define a ConsoleAppender, which will log to stdout/stderr.
catalina.out being the redirection of stdout/stderr used by the Tomcat init script, adding this to your configuration should solve your problem.
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<encoder>
<pattern><![CDATA[
%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80}[%L] - %msg%n
]]></pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/home/my-user/my-log.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE" />
</root>

Bizarre "18 chars" LOGBack behaviour

This is my first time using Logback - normally use log4j - been forced into it by a dependency.
I used the online converter for log4j.properties --> logback.xml.
When I run the application, all I get are the first 18 chars of any log line, without a newline at the end. The resulting output, both on the console and in the log file is:
16:32:00.537 (main16:32:00.537 (main16:32:00.537 (main16:32:00.537
(main16:32:00.537 (main16:32:00.537 (main16:32:00.537 (main16:32:00.537 (main
What on earth could possibly be causing this?
The logback.xml is
<configuration>
<appender name="xyzzy" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>xyzzy.log</File>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{HH:mm:ss.SSS} (%t) %-5p [%c{36}] - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xyzzy.log.%d</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%d{HH:mm:ss.SSS} (%t) [%c{36}] %msg%n</pattern>
</encoder>
</appender>
<logger name="com.package" level="DEBUG"/>
<logger name="org.apache.http.client.protocol.ResponseProcessCookies" level="ERROR"/>
<root level="WARN">
<appender-ref ref="xyzzy"/>
<appender-ref ref="console"/>
</root>
</configuration>
I don't know LOGBack but, according to the documentation,
I'm pretty sure that
(%t)
should be
[%t]
and it is exactly the point where your pattern breaks...
Try then with:
<pattern>%d{HH:mm:ss.SSS} [%t] %-5p [%c{36}] - %msg%n</pattern>
[...]
<pattern>%d{HH:mm:ss.SSS} [%t] [%c{36}] %msg%n</pattern>
assuming all the rest is fine...
In logback, parenthesis in within the pattern string serve as grouping tokens. They need to be escaped.

Categories