In Logback, the log level of a logger can be changed with setLevel() method. But in Logback, because the loggers are singleton, the call of setLevel() method will affect to all other threads that use same logger.
Now I have a class used in a web application like this:
class FooService {
private void insertRecord(Foo foo) {
// insert one record to DB
}
public void insertOne(Foo foo) {
insertRecord(foo);
}
public void insertMany(List<Foo> foos) {
// I want to stop logging here
for (Foo foo: foos) {
insertRecord(foo);
}
// I want to resume logging here
}
}
And in Spring ApplicationConfig.xml:
<bean id="traceAdvice"
class="org.springframework.aop.interceptor.CustomizableTraceInterceptor">
<property name="enterMessage"
value="Entering $[targetClassShortName].$[methodName]($[argumentTypes])[$[arguments]]" />
<property name="exitMessage"
value="Exiting $[targetClassShortName].$[methodName] : return = $[returnValue] : time = $[invocationTime]ms" />
<property name="exceptionMessage"
value="Exception thrown in $[targetClassShortName].$[methodName] : $[exception]" />
</bean>
<aop:config>
<aop:pointcut id="pointcut-service"
expression="execution(* my.app.service..*Service.*(..))" />
<aop:advisor advice-ref="traceAdvice" pointcut-ref="pointcut-service" />
</aop:config>
I want to log call of insertRecord from insertOne method. On the other hand, in insertMany method, I want to stop logging just before the loop (because it might output enormous amount of logs), and resume logging just after the loop. But if you call setLevel() just before the loop, the change of log level will affect to the other logger used in other threads. In this situation, I think you will get decifient logs on the other threads.
My question is: How to change the log level only for the logger used in current thread?