I'm very new to log back and want to try using it in my app. I tried to configure it as follows:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/file/log.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/rotated/log.log.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history capped at 3GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>16GB</totalSizeCap>
</rollingPolicy>
<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>
When I write a simple application I have a problem that stacktraces of uncaught exceptions are not get logged:
public static void main(String[] args){
logger.info("Test")
logger.error("TEST!")
throw new IllegalArgumentException("Exception")
}
And what I have in the log file is:
16:57:05.905 [main] INFO com.App - Test
16:57:05.907 [main] ERROR com.App - TEST!
How to configure logging stacktraces of uncaught exceptions?
I don't think that you can log uncaught exceptions outside your main() method at all. But what you CAN do is to use a "catch all" block in your main method:
public static void main(String[] args){
try
{
logger.info("Test");
logger.error("TEST!");
someMethodThatPropablyThrowsAnException();
throw new IllegalArgumentException("Exception");
}
catch (Exception exception)
{
logger.error(exception);
}
}
This should log any exception thrown in your code that was not handled yet.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments