logback-classic does not log Java Exceptions - java

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>

Related

Change HikariCP Logging file

I am currently using Sl4j 1.7.25 with logback-classic-1.2.3
The logback.xml put inside the class path of the tomcat i.e WEB-INF/classes is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<root level="ALL">
<appender-ref ref="FILE" />
</root>
</configuration>
This works fine and everything gets logged inside the foodini.log file under tomcat logs directory.
Now unfortunately, HikariCp also logs to the same file and constantly at an interval of around 30 seconds. It basically writes pool events and leaks and all. I need this to go in a separate file say hikari.log rather than inside foodini.log which is meant to be the logs of just my webapp to avoid clutter.
I tried :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<appender name="HIKARI" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/hikari.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<logger name="com.zaxxer.hikari">
<appender-ref ref="HIKARI" />
</logger>
<root level="ALL">
<appender-ref ref="FILE" />
</root>
</configuration>
It does create a new file called hikari.log but all things still get logged inside foodini.log
For any one servlet i use :
private static final Logger LOG = LoggerFactory.getLogger(ServeletName.class);
to get the logger and it works fine.
EDIT: added a logger configuration to the logback file and now logs go to both the files foodini.log and hikari.log... Now i need the hikari logs from going in foodini.log file at all
For anyone interested, the configuration logback.xml i created is nearly correct, only one thing was missing and that was the additivity property on the logger element. Without that set to false, the hikari logs would go to both files instead of exclusively going to hikari.log files, heres the updated and working configuration example to get two different log file, one fore the tomcat webapp and one for hikari config,pool and error logs
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/foodini.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<appender name="HIKARI" class="ch.qos.logback.core.FileAppender">
<file>${catalina.home}/logs/hikari.log</file>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>[%d{dd-MMM-yyyy HH:mm:ss.SSS}] [%logger] [%-5level] - %msg%n</Pattern>
</encoder>
</appender>
<logger name="com.zaxxer.hikari" level="ALL" additivity="false">
<appender-ref ref="HIKARI" />
</logger>
<root level="ALL">
<appender-ref ref="FILE" />
</root>

How can I get logback to write to a log file outside of my jar?

I have built a jar file, and inside it is the logback.xml config file which looks like this:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>output.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="ERRORS" class="ch.qos.logback.core.FileAppender">
<file>errors.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="error.logger" level="ERROR">
<appender-ref ref="ERRORS" />
</logger>
<logger name="info.logger" level="DEBUG">
<appender-ref ref="FILE" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE"/>
<appender-ref ref="ERRORS"/>
</root>
However, when I run my jar like this java -jar myapp.jar it never writes to any log file. What am I missing? Is it possible to write to a log file outside of the jar using logback? How do I configure this?

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 ?
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>

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>

Logback - delete the log file on startup

I want to delete the log file each time the program is started as opposed to it being appended. I've tried using the cleanHistoryOnStart property but that seems to have no effect whatsoever. I'm probably missing something here.
I am on Linux and using Eclipse if that matters.
<?xml version="1.0" encoding="utf-8"?>
<configuration scan="true" scanPeriod="10 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{dd.MM.yyyy. HH:mm:ss} %level [%thread] %logger{20} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>chat.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>chat.log.%d{yyyy-MM-dd}</fileNamePattern>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<pattern>%d{dd.MM.yyyy. HH:mm:ss} %level [%thread] %logger{20} - %msg%n</pattern>
</encoder>
<Encoding>utf-8</Encoding>
</appender>
<logger name="src" additivity="false" level="ALL">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
<root level="OFF">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Include <param name="Append" value="false" /> in your appender to clean the log file on start up.
Otherwise try this one
http://veerasundar.com/blog/2009/08/how-to-create-a-new-log-file-for-each-time-the-application-runs/

Categories