AspectLogStart.java 2.01 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.log.Log;
import com.yanzuoguang.util.vo.LogVo;
import org.springframework.stereotype.Component;

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

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

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

    /**
     * 记录请求日志
     *
     * @param tag
     * @param url
     * @param requestBody
     * @param log
     * @param logFlag
     * @return
     */
    // @Async
    public void requestLog(Class<?> cls, String tag, String url, Object requestBody, LogVo log, boolean logFlag) {
        try {
            String fullUrl = String.format("%s:%s", tag, url);
            logCountTime.start(fullUrl);

            String body = aspectLogBody.getBodyString(requestBody);
            if (logFlag) {
                this.logLocal.startLog(log, tag, String.format("%s:%s", this.cloudConfig.getApplicationName(), url), body);
            }
            boolean isLog = logFlag && this.cloudConfig.isLogCommon();
            if (isLog) {
                Log.info(cls, " %s [ %s ] request: %s", tag, url, body);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 获取是否清空日志的标签
     *
     * @return 返回日志是否清空
     */
    protected boolean requestLogInit() {
        boolean clear = Log.threadCurrent() == null;
        if (clear) {
            Log.threadBegin();
        }
        return clear;
    }


}