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

<aop:aspectj-autoproxy/>
<bean class = "com.liyang.robust.LogAspect"/>



@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<String , String[]> 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<String , Object> 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<String , Object> 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  ; 
	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值