我需要以特定的消息格式记录消息,例如:messageFormat =“applicationName = {},methodSignature = {},response = {}”messageFormat =“applicationName = {},methodSignature = {},request = {}”so我们可以使用splunk.Iam使用logback来创建仪表板来实现它 . 所以我创建了自定义 Logger ,它实现了SLF4J Logger ,如下所示:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public  class MyLogger implements Logger {      
private  static Logger log;

public MyLogger(Class<?> name){
    log = LoggerFactory.getLogger(name);
}

/**
 * used to log request in the methods at debug level with messageFormat = "applicationName={},methodSignature={},request={}"
 * @param methodSignature
 * @param request
 */
public void logDebugRequest(String methodSignature,String request){
    String messageFormat = "applicationName={},methodSignature={},request={}";
    debug(messageFormat,"applicationname",methodSignature,request);
}

/**
 * used to log response from methods at debug level with messageFormat = "applicationName={},methodSignature={},response={}"
 * @param methodSignature
 * @param response
 */
public void logDebugResponse(String methodSignature,Object response){
    String messageFormat = "applicationName={},methodSignature={},response={}";
    debug(messageFormat,"applicationname",methodSignature,response);
}
@Override
public void debug(String arg0, Object... arg1) {
    log.debug(arg0, arg1);

}

@Override
public void error(String arg0, Throwable arg1) {
    log.error(arg0,arg1);

}
..//other overridden methods from slf4j

}

在我的项目中,我使用日志记录方面类来记录请求和方法的响应,如下所示:

public  class CommonLoggingAspect {
private final MyLogger log = new MyLogger(CommonLoggingAspect.class);
// this advice runs around the method execution and is used to log the request, response of methods
@Around("within(com.product.rest.productresource)")
protected Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    StringBuilder methodSignature = new StringBuilder();
    methodSignature.append(joinPoint.getSignature().getDeclaringTypeName()).append(".").append(joinPoint.getSignature().getName()).append("()");
    log.logDebugRequest(methodSignature.toString(), Arrays.toString(joinPoint.getArgs()));

    try {
        Object result = joinPoint.proceed();
        log.logDebugResponse(methodSignature.toString(), result);

        return result;
    } catch (IllegalArgumentException e) {
        log.logError(methodSignature.toString(), e.getClass().getName(), "Illegal arguments " + Arrays.toString(joinPoint.getArgs()));
        throw e;
    }    
} }

在Productresource类方法中,我记录了一些消息以及示例: -

Public class ProductResource{
MyLogger log = new MyLogger(ProductResource.class);
public String method1(string req1){
int x=0;
log.debug("value of x is " + x);
return "ss";
}

public String method2(string req2){
int y=0;
log.debug("value of y is " + y);
return "yy";
}

}

In the log file I can see the msgs printed as :
class name                            message
com.product.CommonLoggingAspect -     applicationName=product,methodSignature=ProductResource.method1,request="req1"
com.product.CommonLoggingAspect - value of x is  0 //this message is logged     in ProductResource class, but the class name is printed as CommonLoggingAspect
com.product.CommonLoggingAspect -    applicationName=product,methodSignature=ProductResource.method1,response=ss

所以我的问题是:有什么方法可以实现特定消息格式的登录 . 在我目前的方法中,我使用自定义 Logger 实现了slf4j logger,在我的类中,我创建了一个新的MyLogger实例,如下所示:

MyLogger log = new MyLogger(ProductResource.class);

这对我来说似乎不对,因为我没有编写自己的LoggerFactory类 . 结果,Iam创建了多个logger实例,并运行了诸如在日志中打印不正确的类名等问题,并且还遇到了锁定问题 . 我想找一个LoggerFactory和StaticLoggerBinder类的例子 . 请你提出任何建议 .