结果日志实体类 package com.cheyipai.platformservice.thirdparty.aop;import com.cheyipai.platformservice.thirdparty.utils.OSUtil;import java.io.Serializable;/** * big log * mwang * 2017-07-27 */public class BigLogObj implements Seriali
package com.cheyipai.platformservice.thirdparty.aop; import com.cheyipai.platformservice.thirdparty.utils.OSUtil; import java.io.Serializable; /** * big log * mwang * 2017-07-27 */ public class BigLogObj implements Serializable { private static final long serialVersionUID = 1L; private String table = "big_log"; private String appname = "third-party-service-api"; //应用 private String requestParams; //请求参数 private String responseContent; //返回数据 private String url; //请求url private String serverIp = OSUtil.getLocalIP();//访问者ip private String httpMethod; //请求类型 private long execTime; //执行时长 private String logTime; //记录日志时间 private long logTimeStamp; //记录时间戳 private String desc; //描述信息 public BigLogObj() { } public BigLogObj(String requestParams, String responseContent, String url, String httpMethod, long execTime, String logTime, String desc) { this.requestParams = requestParams; this.responseContent = responseContent; this.url = url; this.httpMethod = httpMethod; this.execTime = execTime; this.logTime = logTime; this.desc = desc; } public String getTable() { return table; } public void setTable(String table) { this.table = table; } public String getAppname() { return appname; } public void setAppname(String appname) { this.appname = appname; } public String getRequestParams() { return requestParams; } public void setRequestParams(String requestParams) { this.requestParams = requestParams; } public String getResponseContent() { return responseContent; } public void setResponseContent(String responseContent) { this.responseContent = responseContent; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getServerIp() { return serverIp; } public void setServerIp(String serverIp) { this.serverIp = serverIp; } public String getHttpMethod() { return httpMethod; } public void setHttpMethod(String httpMethod) { this.httpMethod = httpMethod; } public long getExecTime() { return execTime; } public void setExecTime(long execTime) { this.execTime = execTime; } public String getLogTime() { return logTime; } public void setLogTime(String logTime) { this.logTime = logTime; } public long getLogTimeStamp() { return logTimeStamp; } public void setLogTimeStamp(long logTimeStamp) { this.logTimeStamp = logTimeStamp; } public static long getSerialversionuid() { return serialVersionUID; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }环绕切面
package com.cheyipai.platformservice.thirdparty.aop; import com.alibaba.fastjson.JSONObject; import com.cheyipai.platformservice.thirdparty.utils.DateUtil; import org.aspectj.lang.ProceedingJoinPoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Date; /** * Controller切面--打印请求参数日志 * mwang * 2017-07-27 */ public class ControllerAspect { protected static Logger logger = LoggerFactory.getLogger("channelLogger"); public Object around(ProceedingJoinPoint pjp) { long start = System.currentTimeMillis();//开始时间 long end = 0;//结束时间 HttpServletRequest request = null;//request Object obj = null;//response try{ request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); obj = pjp.proceed(); end = System.currentTimeMillis();//结束时间 logger.info("around " + pjp + "\tUse time : " + (end - start) + " ms!"); }catch (Throwable t){ logger.info("ControllerAspect.around()报错,错误信息:" + t.getMessage()); }finally{ //记录日志 long execTime = end - start; this.recordBigLog(request, obj, execTime); return obj; } } //记录big log private void recordBigLog(HttpServletRequest request, Object obj, long execTime) { if(request == null){ return; } //记录big log BigLogObj bigLogObj = new BigLogObj(); //拼接请求参数 StringBuffer sb = new StringBuffer(); for(Object key : request.getParameterMap().keySet()){ sb.append(key).append("=").append(request.getParameter(key.toString())).append("&"); } bigLogObj.setRequestParams(sb.toString());//请求参数 bigLogObj.setResponseContent(JSONObject.toJSONString(obj));//返回数据 bigLogObj.setUrl(request.getRequestURL().toString());//请求url bigLogObj.setHttpMethod(request.getMethod());//请求类型 bigLogObj.setExecTime(execTime);//执行时长 bigLogObj.setLogTime(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));//记录日志时间 bigLogObj.setLogTimeStamp(System.currentTimeMillis());//记录时间戳 logger.info(JSONObject.toJSONString(bigLogObj)); } }spring-aop配置
自定义log4j配置