开发者

slf4j: debug behavior inconsistent?

开发者 https://www.devze.com 2023-03-01 02:36 出处:网络
I\'m using slf4j + log4j with a library of mine. I am using it in a quick application where I don\'t really care about configuring the logging... or at least I didn\'t think I cared.

I'm using slf4j + log4j with a library of mine.

I am using it in a quick application where I don't really care about configuring the logging... or at least I didn't think I cared.

If I don't configure log4j, I get the standard warning message

log4j:WARN No appenders could be found for logger com.xyz.abcdbabble
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for开发者_JAVA技巧 more info.

which is fine. If I then do

logger.debug("Blah blah blah")

then the logger doesn't print anything out. Great!

But if I do

if (logger.isDebugEnabled())
{
    System.out.println("print some complex stuff:");
    print_some_complex_stuff();
}

then the stuff in brackets gets executed. What gives?

I'm looking for a way to determine whether to print out something that should have an equivalent enabling to logger.debug(), even if log4j isn't configured in the end application. How can I do this?


Instead of trying to figuring out the logic of the log4j default configuration, I would put in a config file (log4j.properties), like:

log4j.rootLogger=debug, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n

At least this puts you into control.


Instead of bundling with log4j then bundle with the "simple" backend from the slf4j distribution. Then things work as you want them to, and the users cannot tinker.

0

精彩评论

暂无评论...
验证码 换一张
取 消