如何在Logback日志记录中覆盖方法名称? [英] How to Override method name in Logback logging?

查看:500
本文介绍了如何在Logback日志记录中覆盖方法名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AOP来实现日志记录概念,但是在打印日志时,我需要提供自己的方法名称而不是默认名称.

I am trying to implement a logging concept with AOP but while printing the log I need give my own method name instead of the default.

更新(基于@glitch的评论):

Update (based on the comments from @glitch):

  • 我正在使用%M转换说明符告诉Logback在每个日志事件中包括方法名称.

  • I am using the %M conversion specifier to tell Logback to include the method name in each log event.

我想专门为 some 日志事件替换Logback派生的方法名称;对于我的AOP连接点发出的日志事件.

I want to replce the Logback derived method name for some log events, spcifically; for the log events emitted by my AOP joinpoint.

我不想在日志事件中的其他地方写入实际方法名称";我希望使用方法名称并使其正确无误,即代表原始方法而不是拦截方法.

I do not want to write the 'actual method name' somewhere else in the log event; I want the method name to be used and to be correct i.e. to represent the original method rather than the interception method.

推荐答案

%M转换说明符由ch.qos.logback.classic.pattern.MethodOfCallerConverter实现.实现非常简单:

The %M conversion specifier is implemented by ch.qos.logback.classic.pattern.MethodOfCallerConverter. The implementation is quite simple:

public String convert(ILoggingEvent le) {
    StackTraceElement[] cda = le.getCallerData();
    if (cda != null && cda.length > 0) {
        return cda[0].getMethodName();
    } else {
        return CallerData.NA;
    }
}

因此,您可以提供您自己的实现.大概是这样的...

So, you could provide your own implementation. Something like this perhaps ...

public class CustomMethodOfCallerConverter extends ClassicConverter {

    public String convert(ILoggingEvent le) {
        StackTraceElement[] cda = le.getCallerData();
        if (cda != null && cda.length > 0) {
            if (le.getMDCPropertyMap().containsKey("CUSTOM_METHOD_NAME_KEY")) {
                String methodName = le.getMDCPropertyMap().get("CUSTOM_METHOD_NAME_KEY");
                // remove the MDC entry since we are only using MDC to pass the custom method name into this converter
                le.getMDCPropertyMap().remove("CUSTOM_METHOD_NAME_KEY");
                return methodName;
            } else {
                return cda[0].getMethodName();
            }
        } else {
            return CallerData.NA;
        }
    }
}

...,它使用MDC从您的联接点传递实际的方法名称.在连接点中,您需要在调用记录器之前先输入MDC值,例如

... which uses MDC to pass the actual method name from your joinpoint. In your joinpoint you would put the MDC value before invoking on the logger e.g.

MDC.put("CUSTOM_METHOD_NAME_KEY", pjp.getSignature().getName()));

但是... Logback不提供任何方式让您声明自己的自定义转换器. Logback使用的转换器在以下位置的静态初始化器中声明: ch.qos.logback.classic.PatternLayout,并且这是不可扩展/可替代的.所以,我认为您的选择是:

But ... Logback does not provide any way for you to declare your own custom converter. The converters in use by Logback are declared in a static initializer on ch.qos.logback.classic.PatternLayout and this is not extensible/overrideable. So, I think your options are:

  1. 在您自己的代码库中创建ch.qos.logback.classic.pattern.MethodOfCallerConverter类,即用自己的Logback自己的MethodOfCallerConverter替换.您可以使用上面提供的示例,并且-只要在调用记录器之前在MDC中输入CUSTOM_METHOD_NAME_KEY值-它就会完成您想要的操作

  1. Create a ch.qos.logback.classic.pattern.MethodOfCallerConverter class in your own code base i.e. replace Logback's own MethodOfCallerConverter with your own. You could use the example I provided above and - as long as you put the CUSTOM_METHOD_NAME_KEY value in MDC before invoking on the logger - it will do what you want

继续使用%M说明符,但对于AOP拦截的方法,添加一个附加的MDC属性以显示实际的方法名称.这将导致Logback方法名称出现在所有日志输出中,而actula方法名称也会出现(如果可用).例如:

Continue to use the %M specifier but for AOP intercepted methods add an additional MDC attribute to display the actual method name. This would result in the Logback method name appearing in all log output with the actula method name appearing too (when available). For example:

// put the actual method name in MDC
MDC.put("actualMethodName", pjp.getSignature().getName());

// specify your pattern - in logback.xml - to include the actual method name
%d{yyyy-MM-dd HH:mm:ss}|[%thread]|%-5level|%logger{36}|%M%X{actualMethodName:-}|%msg%n

  • 使用%M指示符停止,并通过MDC记录所有方法名称.这将导致显示正确的方法名称,但需要您在每个方法中更新MDC(听起来很尴尬).例如:

  • Stop using the %M specifier and log all method names via MDC. This would result in the correct method name appearing but it would require you to update MDC in every method (which sounds quite awkward). For example:

    // put the actual method name in MDC
    MDC.put("actualMethodName", pjp.getSignature().getName());
    
    // specify your pattern - in logback.xml - to include the actual method name
    %d{yyyy-MM-dd HH:mm:ss}|[%thread]|%-5level|%logger{36}|%X{actualMethodName}|%msg%n
    

  • 这篇关于如何在Logback日志记录中覆盖方法名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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