AspectLogResult.java 3.55 KB
package com.yanzuoguang.cloud.aop;

import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.log.LogLocal;
import com.yanzuoguang.log.LogCountTime;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.LogVo;
import com.yanzuoguang.util.vo.ResponseResult;
import org.springframework.stereotype.Component;

/**
 * 写入日志
 *
 * @author 颜佐光
 */
@Component
public class AspectLogResult {

    private final CloudConfig cloudConfig;
    private final LogLocal logLocal;
    private final AspectLogBody aspectLogBody;
    private final LogCountTime logCountTime;


    private final ResponseResult<String> responseDefault = ResponseResult.result("操作成功");

    public AspectLogResult(CloudConfig cloudConfig, LogLocal logLocal, AspectLogBody aspectLogBody, LogCountTime logCountTime) {
        this.cloudConfig = cloudConfig;
        this.logLocal = logLocal;
        this.aspectLogBody = aspectLogBody;
        this.logCountTime = logCountTime;
    }

    /**
     * 保存日志
     *
     * @param cls         类型
     * @param tag         标签名称
     * @param url         地址
     * @param logFlag     请求日志
     * @param start       开始时间
     * @param requestBody 请求内容
     * @param result      结果
     * @param resultEx    异常结果
     * @param log         内容
     */
    // @Async
    public void responseLog(Class<?> cls, String tag, String url, boolean logFlag, long start,
                            Object requestBody, Object result, Exception resultEx, LogVo log) {
        // 执行时间
        long time = System.currentTimeMillis() - start;
        // 全路径
        String fullUrl = String.format("%s:%s", tag, url);

        try {
            // 输出日志标记
            boolean isLogCommon = logFlag && this.cloudConfig.isLogCommon();
            boolean isError = resultEx != null;
            boolean isLogDisplay = isLogCommon || isError;
            // 记录请求时间
            logCountTime.finish(fullUrl, time, isError);
            // ThreadHelper.waitRun(WAIT_MAX, WAIT_ITEM, k -> StringHelper.isEmpty(log.getLogId()))
            if (StringHelper.isEmpty(log.getLogId())) {
                return;
            }
            if (!isLogDisplay) {
                logLocal.remove(log);
                return;
            }
            String body = aspectLogBody.getBodyString(requestBody);
            if (!StringHelper.compare(log.getContent(), body)) {
                log.setContentTo(body);
            }
            // 处理结果
            ResponseResult<?> responseResult;
            if (result instanceof ResponseResult) {
                responseResult = (ResponseResult<?>) result;
            } else if (result != null) {
                responseResult = ResponseResult.result(result);
            } else if (resultEx != null) {
                responseResult = ExceptionHelper.getError(resultEx);
            } else {
                responseResult = responseDefault;
            }

            String json = JsonHelper.serialize(responseResult);
            Log.error(cls, resultEx, "%s [ %s ] time %d ms, result: %s", tag, url, time, aspectLogBody.getMaxString(json));
            logLocal.result(log, responseResult.getCode(), json, isError);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (logFlag) {
                Log.threadCommit();
            }
        }
    }

}