如何使用 Logback 仅更改当前线程的日志级别

SAT*_*uke 5 java logging logback

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

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)

在 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] with return value $[returnValue], took $[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>
Run Code Online (Sandbox Code Playgroud)

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

我的问题是:如何仅更改当前线程中使用的记录器的日志级别?

SAT*_*uke 8

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

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

<configuration>
    <turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
      <MDCKey>tracing</MDCKey>
      <Value>off</Value>
      <OnMatch>DENY</OnMatch>
    </turboFilter>
    ......
</configuration>
Run Code Online (Sandbox Code Playgroud)

您可以通过向 MDC 添加/删除键和值来打开/关闭日志记录,如下所示(请注意,您应该考虑实际使用中的异常):

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您想停止 TRACE/DEBUG/INFO/WARN 日志但保持 ERROR 日志处于活动状态,您可以使用 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>
Run Code Online (Sandbox Code Playgroud)