简体   繁体   中英

How to apply log pattern to both SLF4J _and_ JUL?

I'm using logback and the JUL to SLF4J bridge (the latter for capturing third party libraries log statements that use Java Logging).

I get the log output from the third parties, but the logging pattern isn't what I specified for Logback. Should JUL and Logback use the same pattern that I specified in my logback.xml? Or do I need to set JUL separately? And finally, if I need to set JUL separately, is there a way to have a single place to define my log output pattern and have that go to JUL and Logback?

The setup I have looks like this:

In my main class I have a static block (at the top of the class) with these lines:

    SLF4JBridgeHandler.removeHandlersForRootLogger();
    SLF4JBridgeHandler.install();

My logback.xml looks like this:

<configuration>
    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <Pattern>
                %d{HH:mm:ss.SSS} [%-32thread] %-5level | %-80msg | %class.%method\(%file:%line\)%n
            </Pattern>
        </encoder>
    </appender>
    <root level="WARN">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

Below are two lines of log output that I copied. You'll notice the first line is per the format I specified and the second line is not (because it is coming from JUL):

21:06:33.297 [ModelUpdateThread-1             ] INFO  | view Molecules and Complexes took 1 ms for 17116 model elements                  | com.**[blanked out for privacy]**
Sat Nov 21 21:06:33 EST 2015 : com.tomsawyer.licensing.TSILicenseManager - setUseProxy
INFO: setUseProxy: false

Patterns set in Logback should be automatically applied to JUL... or rather, since the JUL log records are handed off to Logback for logging the pattern will apply.

The problem I was seeing turned out to be because the third party library (from tomsawyer) was by default writing its logs to both stdout and JUL. That is... Tomsawyer was using System.out to write log messages in addition to Logger.info . The clue that this was happening resides in the log level of my configuration (warn) and the output log level from Tomsawyer (info).

So indeed the Tomsawyer output was being sent to logback and then suppressed because the log level was configured at warn. And since Tomsawyer was also outputting to stdout with System.out ... I still saw the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM