sviolet.thistle.x.util.trace.DefaultTraceProvider Maven / Gradle / Ivy
/*
* Copyright (C) 2015-2019 S.Violet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project GitHub: https://github.com/shepherdviolet/thistle
* Email: [email protected]
*/
package sviolet.thistle.x.util.trace;
import sviolet.thistle.util.judge.CheckUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* 全局追踪默认实现
*
*
* 1.追踪号和其他追踪信息保存在ThreadLocal中.
* 2.如果应用依赖SLF4J, 追踪号还会存入MDC, KEY为{@value Trace#TRACE_ID_KEY}, 可以打印在日志中.
*
*
* @author zhuqinchao
*/
public class DefaultTraceProvider implements TraceProvider {
private static final TraceIdProvider TRACE_ID_PROVIDER;
static {
TraceIdProvider traceIdProvider;
try {
//尝试用SLF4J存追踪号
Class.forName("org.slf4j.Logger");
traceIdProvider = (TraceIdProvider) Class.forName("sviolet.thistle.x.util.trace.Slf4jTraceIdProvider").newInstance();
} catch (Exception e) {
traceIdProvider = new LocalTraceIdProvider();
}
TRACE_ID_PROVIDER = traceIdProvider;
}
private ThreadLocal