2015-09-20 41 views
-2
  1. 大家好,我已經記錄所有的請求,響應異常, 錯誤我Spring服務。我已經搜查有關 攔截器,過濾器,過濾器的日誌記錄,Spring攔截: HandlerInterceptorAdapter記錄器過濾器: AbstractRequestLoggingFilter.java和它的子類 (http://www.javawebdevelop.com/1704067/), CommonsRequestLoggingFilter.java過濾器:LoggerFilter。記錄請求和響應Spring MVC的服務

    任何機構的差異,最好的辦法這樣做,我很困惑 或我需要找出第三方庫做到這一點 ?..

回答

0

Log4j是基於java的日誌工具與Spring MVC輕鬆整合。 Log4j具有不同的日誌記錄級別,可以根據開發環境進行適當的日誌記錄。

Log4j 2是log4j的繼任者,與其前身相比具有更好的性能。

請參閱下面的鏈接爲彈簧MVC +的Log4j
http://www.codejava.net/frameworks/spring/how-to-use-log4j-in-spring-mvc

編輯的積分:如在評論中提及,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)); 
} 

}

+0

沒有,我婉登錄請求和響應休息..感謝反正.. – ramki

+0

請參見上面的編輯。我希望我已經回答了你的問題。 – manu

0

我用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; 
    } 
}