Commit 5ef909cf authored by duanruiming's avatar duanruiming

[update] 优化系统日志打印格式,添加接口消耗时间

parent c52bc3e3
...@@ -14,6 +14,7 @@ import org.aspectj.lang.annotation.Aspect; ...@@ -14,6 +14,7 @@ import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.NamedThreadLocal;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
...@@ -33,11 +34,15 @@ import static net.wanji.common.framework.Constants.DATE_FORMAT.E_DATE_FORMAT_SEC ...@@ -33,11 +34,15 @@ import static net.wanji.common.framework.Constants.DATE_FORMAT.E_DATE_FORMAT_SEC
@Aspect @Aspect
@Slf4j @Slf4j
@Component @Component
@SuppressWarnings("all")
public class LogAspectHandler { public class LogAspectHandler {
@Value("${spring.application.name}") @Value("${spring.application.name}")
private String serverName; private String serverName;
/** 计算操作消耗时间 */
private static final ThreadLocal<Long> TIME_THREADLOCAL = new NamedThreadLocal<Long>("Cost Time");
/** /**
* @AspectLog自定义注解标记的controller * @AspectLog自定义注解标记的controller
*/ */
...@@ -53,6 +58,7 @@ public class LogAspectHandler { ...@@ -53,6 +58,7 @@ public class LogAspectHandler {
@Before("controllerAspect()") @Before("controllerAspect()")
public void executeBefore(JoinPoint joinPoint) { public void executeBefore(JoinPoint joinPoint) {
try { try {
TIME_THREADLOCAL.set(System.currentTimeMillis());
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = IpAddressUtil.getIpAddress(request); String ip = IpAddressUtil.getIpAddress(request);
Map<String, String> map = getControllerMethodDescription(joinPoint); Map<String, String> map = getControllerMethodDescription(joinPoint);
...@@ -67,7 +73,7 @@ public class LogAspectHandler { ...@@ -67,7 +73,7 @@ public class LogAspectHandler {
//打印操作日志 //打印操作日志
this.consoleLog(log, request); this.consoleLog(log, request);
} catch (Exception e) { } catch (Exception e) {
log.error("打印日志信息日志切面执行异常,异常信息: {}", e); log.error("打印日志信息日志切面执行异常,异常信息:", e);
} }
} }
...@@ -99,10 +105,12 @@ public class LogAspectHandler { ...@@ -99,10 +105,12 @@ public class LogAspectHandler {
operateLog.setStatus(String.valueOf(jsonViewObject.getCode())); operateLog.setStatus(String.valueOf(jsonViewObject.getCode()));
operateLog.setResult(JSON.toJSONString(jsonViewObject)); operateLog.setResult(JSON.toJSONString(jsonViewObject));
operateLog.setCreateTime(new Date()); operateLog.setCreateTime(new Date());
log.info("\n外部系统调用本地接口,请求IP = {} || 接口 = {} || 地址 = {} || 响应状态 = {}", ip, map.get("description").toString(), uri, jsonViewObject.getCode()); long costTime = System.currentTimeMillis() - TIME_THREADLOCAL.get();
log.info("\r\n外部系统调用本地接口响应信息:\r\n请求地址:{}\r\n接口名称:{}\r\n请求地址:{}\r\n响应状态:{}\r\r消耗时间:{}ms",
ip, map.get("description"), uri, jsonViewObject.getCode(), costTime);
} catch (Exception e) { } catch (Exception e) {
//记录本地异常日志 //记录本地异常日志
log.error("记录本地接口响应日志异常,异常信息:{}", e); log.error("记录本地接口响应日志异常,异常信息:", e);
} }
} }
...@@ -110,13 +118,13 @@ public class LogAspectHandler { ...@@ -110,13 +118,13 @@ public class LogAspectHandler {
/** /**
* 日志打印模板 * 日志打印模板
*/ */
private String consoleLogTemplate = "\r\n" + private final String consoleLogTemplate = "\r\n" +
"系统:%s\r\n" + "系统简称:%s\r\n" +
"URL:%s\r\n" + "URL:%s\r\n" +
"操作类型:%s\r\n" + "操作类型:%s\r\n" +
"操作描述:%s\r\n" + "接口名称:%s\r\n" +
"操作IP:%s\r\n" + "请求IP:%s\r\n" +
"操作时间:%s\r\n" + "请求时间:%s\r\n" +
"请求参数:%s\r\n"; "请求参数:%s\r\n";
/** /**
...@@ -135,9 +143,9 @@ public class LogAspectHandler { ...@@ -135,9 +143,9 @@ public class LogAspectHandler {
operateLog.getIp(), operateLog.getIp(),
DateUtil.formatDate(operateLog.getCreateTime(), E_DATE_FORMAT_SECOND.getStrFormat()), DateUtil.formatDate(operateLog.getCreateTime(), E_DATE_FORMAT_SECOND.getStrFormat()),
operateLog.getArgs()); operateLog.getArgs());
log.info(logText); log.info("\r\n外部系统调用本地接口请求信息:" + logText);
} catch (Exception e) { } catch (Exception e) {
log.error("打印操作日志异常,异常信息: {}", e); log.error("打印操作日志异常,异常信息:", e);
} }
} }
...@@ -155,7 +163,7 @@ public class LogAspectHandler { ...@@ -155,7 +163,7 @@ public class LogAspectHandler {
String value = request.getParameter(argName); String value = request.getParameter(argName);
requestParams.put(argName, value); requestParams.put(argName, value);
} }
if (requestParams.size() <= 0) { if (requestParams.size() == 0) {
Object[] args = joinPoint.getArgs(); Object[] args = joinPoint.getArgs();
for (Object arg : args) { for (Object arg : args) {
if (null == arg) { if (null == arg) {
...@@ -165,8 +173,7 @@ public class LogAspectHandler { ...@@ -165,8 +173,7 @@ public class LogAspectHandler {
continue; continue;
} }
String argName = arg.getClass().getName(); String argName = arg.getClass().getName();
Object value = arg; requestParams.put(argName, arg);
requestParams.put(argName, value);
} }
} }
return JSON.toJSONString(requestParams); return JSON.toJSONString(requestParams);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment