dmansfield
dmansfield

Reputation: 1128

how can i dynamically, on a per thread basis, change the loglevel using slf4j/logback

I have an application running inside a tomcat6 servlet container using slf4j with log4j although switching to logback is imminent.

The application makes heavy use of hibernate, and for the time being we need the hibernate SQL logging turned on.

However, there is one code path which makes TONS of repetitive (and boring) calls into hibernate and produces TONS of logging.

I'd like to be able change the loglevel for a specific logger, but have that happen only for the currently executing thread.

I've read about MDC, but it seems that can only be used to add additional "labels" on a per-thread basis, but not to change actual logging levels for specific loggers.

Is this doable?

Upvotes: 3

Views: 1471

Answers (2)

hoaz
hoaz

Reputation: 10161

You can create slf4j logger that is based on both class name and thread name:

logger = LoggerFactory.getLogger(getClass().getName() + "." + Thread.currentThread().getName());

And then use different logback configurations for different threads. Also you can cache logger instance in ThreadLocal variable to minimize performance impact.

Upvotes: 1

dratewka
dratewka

Reputation: 2114

Maybe you can implement a filter which ignores the events coming from that specific thread. And as you said you probably can identify the source thread using MDC.

Upvotes: 3

Related Questions