package cn.sh.stc.sict.cloud.common.log.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.sh.stc.sict.cloud.common.core.util.R;
import cn.sh.stc.sict.cloud.common.security.util.SysLogUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * 异步log输出
 */
@Slf4j
public class SysAccessLogUtil {

    public static void log(HttpServletRequest request, ProceedingJoinPoint joinPoint, Object result, StopWatch stopWatch) {
        StringJoiner logInfo = new StringJoiner("\n", "\n", "");
        logInfo.add("====↓=↓=↓===== " + request.getRequestURL());
        logInfo.add("===REQ_INFO=== " + SysLogUtils.getTitle(joinPoint, request));
        logInfo.add("===REQ_ADDR=== " + request.getMethod() + "\t" + request.getRequestURI());
        logInfo.add("===REQ_ARGS=== " + HttpUtil.toParams(request.getParameterMap()));
        logInfo.add("===REQ_TIME=== " + stopWatch.getTotalTimeMillis() + "ms");
        logInfo.add("===RES_DATA=== " + (Objects.isNull(result) ? JSONUtil.toJsonStr(new R<>()) : JSONUtil.toJsonStr(result)));
        log.info(logInfo.toString());
    }
}