Jason S
Jason S

Reputation: 189626

slf4j: debug behavior inconsistent?

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 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?

Upvotes: 0

Views: 423

Answers (2)

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.

Upvotes: 0

Sasha O
Sasha O

Reputation: 3749

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.

Upvotes: 1

Related Questions