如何仅使用Logback更改当前线程的日志级别 [英] How to change log level only for current thread with Logback

查看:607
本文介绍了如何仅使用Logback更改当前线程的日志级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Logback中,可以使用setLevel()方法更改记录器的日志级别. 但是在Logback中,由于记录器是单例记录器,因此setLevel()方法的调用将影响使用同一记录器的所有其他线程.

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.

现在我有一个在Web应用程序中使用的类,如下所示:

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

在Spring ApplicationConfig.xml中:

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>

我想记录来自insertOne方法的insertRecord调用.另一方面,在insertMany方法中,我想在循环之前停止记录(因为它可能会输出大量日志),并在循环之后恢复记录. 但是,如果在循环之前调用setLevel(),日志级别的更改将影响其他线程中使用的其他记录器.在这种情况下,我认为您将在其他线程上获得确定的日志.

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?

推荐答案

我找到了解决方案.为此,您可以使用MDC和TurboFilter. MDC是线程本地的,更改为MDC不会影响其他线程.

I found a solution. You can use MDC and TurboFilter for this purpose. MDC is thread-local and change to MDC does not affect to other threads.

例如,如果要停止所有日志记录活动,则必须在logback.xml中添加MDCFilter的定义(请注意,<turboFilter>标记不能是<appender>标记的子代,而应该是<appender>标记的子代). <configuration>标签的子级):

For example, if you want to stop all the logging activity, you have to add a definition of MDCFilter to logback.xml (Note that <turboFilter> tag cannot be a child of an <appender> tag, and should be a child of <configuration> tag):

<configuration>
    <turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
      <MDCKey>tracing</MDCKey>
      <Value>off</Value>
      <OnMatch>DENY</OnMatch>
    </turboFilter>
    ......
</configuration>

您可以通过这样向MDC放置/删除键和值来打开/关闭日志记录(请注意,在实际使用中应考虑异常):

And you can on/off logging by put/remove a key and a value to MDC like this (Note that you should consider about exception in actual usage):

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
        MDC.put("tracing", "off");
        for (Foo foo: foos) {
            insertRecord(foo);
        }
        // I want to resume logging here
        MDC.remove("tracing");
    }
}

或者,如果要停止TRACE/DEBUG/INFO/WARN日志,但使ERROR日志保持活动状态,则可以使用DynamicThresholdFilter:

Or, if you want to stop TRACE/DEBUG/INFO/WARN log but leave ERROR log active, you can use DynamicThresholdFilter:

<configuration>
    <turboFilter class="ch.qos.logback.classic.turbo.DynamicThresholdFilter">
      <Key>tracing</Key>
      <DefaultThreshold>TRACE</DefaultThreshold>
      <MDCValueLevelPair>
        <value>off</value>
        <level>ERROR</level>
      </MDCValueLevelPair>
    </turboFilter>
    ......
</configuration>

这篇关于如何仅使用Logback更改当前线程的日志级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆