Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to log Uncaught exceptions with Logback

I wrote a web-app with various frameworks(jsf,Spring,Hibernate) and my logger library is Logback and slf4j.

At the moment I'm not able to display uncaught exceptions(for example NullPointers) in log file.

This is my logBack.xml

<configuration debug="true">
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${CATALINA_HOME}/logs/jsfDemo.log</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>jsfDemo.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender> 

    <root level="DEBUG">         
        <appender-ref ref="FILE" />
    </root> 

</configuration>

when I execute this few lines of code with uc=null

public void nullPointerMethod(UserCredential uc){
        LOG.debug(">>login(uc)");

        if(uc == null){
            throw new NullPointerException();
        }else{
            //do something
        }

        LOG.debug("<<login(uc)");
    }

in the logFile I see only

>>login(uc)

but I want to see stackTrace of NullPointer. What's wrong?

like image 466
Gavi Avatar asked Apr 23 '14 14:04

Gavi


1 Answers

You can use SLF4JBridgeHandler:

Essentially, the idea is to install on the root logger an instance of SLF4JBridgeHandler as the sole JUL handler in the system. Subsequently, the SLF4JBridgeHandler instance will redirect all JUL log records are redirected to the SLF4J API based

Basically you only need call SLF4JBridgeHandler.install(); in your main method and configure logback this way:

<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
    <resetJUL>true</resetJUL>
</contextListener>
like image 180
brevleq Avatar answered Oct 11 '22 20:10

brevleq