package com.sonic.common.utils;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

/**
 * @description: 日志工具类
 * @author: code
 * @create: 2020-02-20 15:26
 **/
public class LogUtils {

    /**
     * 日志跟踪标识
     */
    private static final String TRACE_ID = "TRACE_ID";

    /**
     * 设置日志traceId
     */
    public static void setTraceId() {
        String traceId = UUID.randomUUID().toString();
        if (StringUtils.isEmpty(MDC.get(TRACE_ID))) {
            MDC.put(TRACE_ID, traceId);
        }
    }

    /**
     * 获取日志ID
     * @return
     */
    public static String getTraceId() {
        return MDC.get(TRACE_ID);
    }

    /**
     * 删除日志traceId
     */
    public static void removeTraceId() {
        MDC.remove(TRACE_ID);
    }

    public static void setTraceId(String internalTraceId) {
        MDC.put(TRACE_ID, internalTraceId);
    }
}
