Reputation: 1128
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
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