How to enable log-back log stack trace og uncaught exceptions

Some Name

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?

actc

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.

edited at
0

Comments

0 comments
Login to comment

Related

Log stack trace for python warning

log not printing the full stack trace

How to log exception centrally at place of original stack trace?

How to console.log an error with stack trace in node.js?

How to set Stack trace depth of PHP's error log?

How to log stack trace error when ConnectionMultiplexer fails to connect?

How do you write a full stack trace to the log?

How do you print out a stack trace to the console/log in Cocoa?

How to extract timestamp of java stack trace from log file?

How can I get uncaught exceptions to appear in the Django log

How to log uncaught exceptions during Flink job execution

Log “Warning” instead of “Error” with stack trace

JSON doesn't render log stack trace

Log to stdout and get string value of the stack trace

Finding the Stack Trace Log from an EXE that crashes

Log4j2: log stack trace without an exception

System.Diagnostics.Trace - correct way to log exceptions

Iris - How to log erros/exceptions?

How to log unhandled exceptions on sentry?

javascript custom console log function keeping stack trace

Laravel: Is it possible to log stack trace when exception caught, and continue the execution?

Log.e does not print the stack trace of UnknownHostException

Print function log /stack trace for entire program using firebug

Log4j does not print stack trace

Suppressing stack trace dump in Laravel log after App::abort(403)

Java stack trace not printing with log4j2

akka.actor.ActorLogging does not log the stack trace of exception by logback

Getting line numbers from a stack trace in a JVM crash log

How to enable MySQL Query Log?