spring请求数据与返回数据日志记录






@Aspect
public class LogAspect {
	
  private static final Logger logger = Logger.getLogger(LogAspect.class);

  @Around("execution(* com.liyang.robust..*.*(..))")
  public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		long  startTimeMillis = System.currentTimeMillis();
	    RequestAttributes ra = RequestContextHolder.getRequestAttributes() ;
	    ServletRequestAttributes sra = (ServletRequestAttributes)ra ;
	    HttpServletRequest request = sra.getRequest() ;
	    Map inputParamMap = request.getParameterMap() ;
	    String requestPath = request.getRequestURI() ;
	    Object result = pjp.proceed() ; 
	    long endTimeMillis = System.currentTimeMillis();
	    logger.info("\n"
	        +"Begin:"  + "\n"
	        +"Url:" + requestPath  + "\n"
	        +"RequestParams:" + JSONObject.toJSON(inputParamMap) + "\n" 
	        +"ResponseResults:"+ JSONObject.toJSON(result)  + "\n" 
	        +"Cost:" + (endTimeMillis - startTimeMillis) + " ms " + "\n"
	        +"End" + "\n") ;
	    return result ;
  }

}


<2016-11-01 15:20:46:308 [INFO]>[XX] -: 
Begin:
Url:/XX/X/X/XXXXX
RequestParams:{"x":["x"],"x":["x"],"x":["x"]}
ResponseResults:{"totalNumber":0,"numberPerPage":0,"errorCode":"DB_ERROR","currentPage":0,"callStatus":"FAILED","totalPage":0}
Cost:706 ms 
End


private  String send(String url , Map params) throws Exception{
		HttpClient httpClient = new HttpClient();
		PostMethod postMethod = new PostMethod(url);
		postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		NameValuePair[] data = new NameValuePair[params.size()] ;
		int idx = 0 ;
		for(Map.Entry param : params.entrySet()){
			data[idx++] = new NameValuePair(param.getKey() , param.getValue() == null ? "" :  param.getValue().toString() ) ; 
		}
		postMethod.setRequestBody(data);
		int statusCode = -1 ;
		try {
			statusCode = httpClient.executeMethod(postMethod);
		} catch (HttpException e) {
			throw new Exception("can not connet SP") ; 
		} catch (IOException e1) {
			throw new Exception("can not connet SP") ; 
		}
		String returnStr = null ;
		if(statusCode==200){
			try {
				returnStr = postMethod.getResponseBodyAsString()  ;
			} catch (IOException e) {
				throw e ;
			}
		}
		else {
			 throw new Exception("can not connet SP") ; 
		}
		return returnStr  ; 
	}




你可能感兴趣的:(工作足迹)