3

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

This would work for my code, but not for code like hibernate which already creates logger instances however it already does.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.