Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc服务的日志记录请求和响应_Spring_Logging_Service - Fatal编程技术网

Spring mvc服务的日志记录请求和响应

Spring mvc服务的日志记录请求和响应,spring,logging,service,Spring,Logging,Service,大家好,我必须记录所有的请求、响应、异常, 我的Spring服务的错误。我已经到处找过了 拦截器、过滤器、日志过滤器、Spring拦截器: HandlerInterceptor适配器记录器筛选器: AbstractRequestLoggingFilter.java及其子类 (), CommonRequestLoggingFilter.java过滤器:LoggerFilter 任何身体都可以区别它,而最好的方法就是这样做 困惑或者我需要找到第三方库来完成这项工作 ? 是一个基于java的日志实用

大家好,我必须记录所有的请求、响应、异常, 我的Spring服务的错误。我已经到处找过了 拦截器、过滤器、日志过滤器、Spring拦截器: HandlerInterceptor适配器记录器筛选器: AbstractRequestLoggingFilter.java及其子类 (), CommonRequestLoggingFilter.java过滤器:LoggerFilter

任何身体都可以区别它,而最好的方法就是这样做 困惑或者我需要找到第三方库来完成这项工作 ?

是一个基于java的日志实用程序,可以轻松地与SpringMVC集成。Log4j具有不同的日志记录级别,以允许与开发环境相对应的适当日志记录

Log4j 2是Log4j的继承者,与前一代相比具有更好的性能

请参考以下链接了解spring MVC+Log4j的集成

编辑:如注释中所述,PFB是记录请求和响应的代码

@Aspect
@Component
public class ResponseLoggerAspect {

private static final Logger logger = Logger.getLogger("requestResponseLogger");
ExclusionStrategy excludeJsonAnnotation = new JsonIgnoreAnnotationExclusionStrategy();
Gson gson = new GsonBuilder().setExclusionStrategies(excludeJsonAnnotation).create();

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controller() {}

@Pointcut("execution(* *(..))")
public void method() {}

@Pointcut("execution(@com.company.annotation.AddLog * *(..))")
public void Loggable() {}

//This will be caught for only those controller method where @AddLog annotation is written
@Before("Loggable()")
public void printRequestLog(JoinPoint joinPoint) {
    try {
        Object[] argsList = joinPoint.getArgs();
        String str = "[";
        for(Object arg : argsList) {
            if(arg instanceof Object[]) {
                str += Arrays.toString((Object[])arg) + ", ";
            } else {
                str += String.valueOf(arg) + ", ";
            }
        }
        str += "]";
        logger.info("Request args for " + joinPoint.getSignature().getName() + " are : " + str);
    } catch(Exception ex) {
        logger.info("Unable to log request args", ex);
    }
}

//This will be called for all controller methods after returning 
@AfterReturning(pointcut = "controller() && method()", returning="result")
public void afterReturning(JoinPoint joinPoint , Object result)  {
    long start = System.nanoTime();
    try {
        logger.info("Response sent by " + joinPoint.getSignature().getName() + " are : " + gson.toJson(result));
    } catch(Exception ex) {
        logger.error("Returned result cant be converted in JSON " , ex);
    }
    long end = System.nanoTime();
    logger.info("elapsed time : " + (end - start));
}

}

在一个项目中,我将AOP用于类似的东西。我必须写一个这样的课程:

@Component
@Aspect
public class RequestMonitor {

    private static final Logger logger = LoggerFactory.getLogger(RequestMonitor.class);

    @Around("@annotation(org.example.ToBeLogged)")
    public Object wrap(ProceedingJoinPoint pjp) throws Throwable {

        logger.info("Before controller method " + pjp.getSignature().getName() + ". Thread " + Thread.currentThread().getName());
        Object retVal = pjp.proceed();
        logger.info("Controller method " + pjp.getSignature().getName() + " execution successful");

        return retVal;
    }
}

请参见上面的编辑。我希望我已经回答了你的问题。