3

I'm using logback-spring.xml in a spring boot project. I want to log all uncaught exceptions to a file. Basically just direct the output from the console to a file. I've tried several variations of logback-spring.xml.

I tried this

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="com.mine" level="WARN" additivity="false">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </logger>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE">
            <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
                <resetJUL>true</resetJUL>
            </contextListener>
        </appender-ref>
    </root>
</configuration>

with this in my main method SLF4JBridgeHandler.install();

I've tried this from the spring docs

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

this is in my application.yml

logging:
  file: C:\dev\assessment-tool\logs\application.log
  config: classpath:logback-spring.xml

For these it will log all the start up logging messages but when an exception is thrown and not caught it goes to the console and doesn't show up in the log file. How can I make uncaught exceptions go to a log file?

4
  • 1
    It should be easier to redirect the standard output to file. Commented Jul 13, 2017 at 0:21
  • an exception like your main thread is dying, or an exception from a web endpoint? Commented Jul 25, 2018 at 17:30
  • same for me. Micronaut (which is based on spring boot) does not print exceptions neither in file nor in console. Even if I'm explicitly throwing it. Commented Feb 12, 2019 at 9:19
  • 1
    Have you ever solved this problem? Commented Jun 14, 2019 at 7:16

1 Answer 1

2

The problem is on the levels you're assigning to the loggers in the logback-spring.xml file.

According to the logback architecture documentation, (you can check it here) "The effective level for a given logger L, is equal to the first non-null level in its hierarchy, starting at L itself and proceeding upwards in the hierarchy towards the root logger".

You're assigning level "INFO" on your root logger, and level "WARN" on the logger "com.mine". None of them get "called" when there is an exception, ex: logger.error(Exception e).

One solution is assign the level "DEBUG" or "ERROR" to the logger "com.mine" or to the root logger, because they have attached the appender "FILE".

Sign up to request clarification or add additional context in comments.

3 Comments

Did you change the level of the logger?
Yes I tried DEBUG and ERROR on the com.mine logger
The problem not with levels though. Levels are working fine. It's just not printing Exceptions which are not cought

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.