| | |
| | | |
| | | import cn.hutool.core.lang.Dict; |
| | | import cn.hutool.core.map.MapUtil; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.alibaba.ttl.TransmittableThreadLocal; |
| | | import org.dromara.common.core.context.ThreadLocalHolder; |
| | | import org.dromara.common.core.domain.model.LoginUser; |
| | | import org.dromara.common.core.utils.ServletUtils; |
| | | import org.dromara.common.core.utils.SpringUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | |
| | | |
| | | import java.util.Collection; |
| | | import java.util.Map; |
| | | import java.util.StringJoiner; |
| | | |
| | | /** |
| | | * 操作日志记录处理 |
| | |
| | | |
| | | |
| | | /** |
| | | * 计算操作消耗时间 |
| | | * 计时 key |
| | | */ |
| | | private static final ThreadLocal<StopWatch> TIME_THREADLOCAL = new TransmittableThreadLocal<>(); |
| | | private static final String LOG_STOP_WATCH_KEY = "logStopwatch"; |
| | | |
| | | /** |
| | | * 处理请求前执行 |
| | |
| | | @Before(value = "@annotation(controllerLog)") |
| | | public void boBefore(JoinPoint joinPoint, Log controllerLog) { |
| | | StopWatch stopWatch = new StopWatch(); |
| | | TIME_THREADLOCAL.set(stopWatch); |
| | | ThreadLocalHolder.set(LOG_STOP_WATCH_KEY, stopWatch); |
| | | stopWatch.start(); |
| | | } |
| | | |
| | |
| | | String ip = ServletUtils.getClientIP(); |
| | | operLog.setOperIp(ip); |
| | | operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255)); |
| | | operLog.setOperName(LoginHelper.getUsername()); |
| | | LoginUser loginUser = LoginHelper.getLoginUser(); |
| | | operLog.setOperName(loginUser.getUsername()); |
| | | operLog.setDeptName(loginUser.getDeptName()); |
| | | |
| | | if (e != null) { |
| | | operLog.setStatus(BusinessStatus.FAIL.ordinal()); |
| | |
| | | // 处理设置注解上的参数 |
| | | getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult); |
| | | // 设置消耗时间 |
| | | StopWatch stopWatch = TIME_THREADLOCAL.get(); |
| | | StopWatch stopWatch = ThreadLocalHolder.get(LOG_STOP_WATCH_KEY); |
| | | stopWatch.stop(); |
| | | operLog.setCostTime(stopWatch.getTime()); |
| | | // 发布事件保存数据库 |
| | |
| | | log.error("异常信息:{}", exp.getMessage()); |
| | | exp.printStackTrace(); |
| | | } finally { |
| | | TIME_THREADLOCAL.remove(); |
| | | ThreadLocalHolder.remove(LOG_STOP_WATCH_KEY); |
| | | } |
| | | } |
| | | |
| | |
| | | * 参数拼装 |
| | | */ |
| | | private String argsArrayToString(Object[] paramsArray, String[] excludeParamNames) { |
| | | StringBuilder params = new StringBuilder(); |
| | | if (paramsArray != null && paramsArray.length > 0) { |
| | | for (Object o : paramsArray) { |
| | | if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { |
| | | try { |
| | | String str = JsonUtils.toJsonString(o); |
| | | Dict dict = JsonUtils.parseMap(str); |
| | | if (MapUtil.isNotEmpty(dict)) { |
| | | MapUtil.removeAny(dict, EXCLUDE_PROPERTIES); |
| | | MapUtil.removeAny(dict, excludeParamNames); |
| | | str = JsonUtils.toJsonString(dict); |
| | | } |
| | | params.append(str).append(" "); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | StringJoiner params = new StringJoiner(" "); |
| | | if (ArrayUtil.isEmpty(paramsArray)) { |
| | | return params.toString(); |
| | | } |
| | | for (Object o : paramsArray) { |
| | | if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { |
| | | String str = JsonUtils.toJsonString(o); |
| | | Dict dict = JsonUtils.parseMap(str); |
| | | if (MapUtil.isNotEmpty(dict)) { |
| | | MapUtil.removeAny(dict, EXCLUDE_PROPERTIES); |
| | | MapUtil.removeAny(dict, excludeParamNames); |
| | | str = JsonUtils.toJsonString(dict); |
| | | } |
| | | params.add(str); |
| | | } |
| | | } |
| | | return params.toString().trim(); |
| | | return params.toString(); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } else if (Map.class.isAssignableFrom(clazz)) { |
| | | Map map = (Map) o; |
| | | for (Object value : map.entrySet()) { |
| | | Map.Entry entry = (Map.Entry) value; |
| | | return entry.getValue() instanceof MultipartFile; |
| | | for (Object value : map.values()) { |
| | | return value instanceof MultipartFile; |
| | | } |
| | | } |
| | | return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse |
| | | || o instanceof BindingResult; |
| | | || o instanceof BindingResult; |
| | | } |
| | | } |