All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mzt.logapi.context.LogRecordContext Maven / Gradle / Ivy

There is a newer version: 3.0.6-oxadmin
Show newest version
package com.mzt.logapi.context;

import com.google.common.collect.Maps;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author muzhantong
 * create on 2021/2/9 2:22 下午
 */
public class LogRecordContext {

    private static final InheritableThreadLocal>> variableMapStack = new InheritableThreadLocal<>();

    public static void putVariable(String name, Object value) {
        if (variableMapStack.get() == null) {
            Stack> stack = new Stack<>();
            variableMapStack.set(stack);
        }
        Stack> mapStack = variableMapStack.get();
        if (mapStack.size() == 0) {
            variableMapStack.get().push(new HashMap<>());
        }
        variableMapStack.get().peek().put(name, value);
    }

    public static Object getVariable(String key) {
        Map variableMap = variableMapStack.get().peek();
        return variableMap.get(key);
    }

    public static Map getVariables() {
        Stack> mapStack = variableMapStack.get();
        return mapStack.peek();
    }

    public static void clear() {
        if (variableMapStack.get() != null) {
            variableMapStack.get().pop();
        }
    }

    /**
     * 日志使用方不需要使用到这个方法
     * 每进入一个方法初始化一个 span 放入到 stack中,方法执行完后 pop 掉这个span
     */
    public static void putEmptySpan() {
        Stack> mapStack = variableMapStack.get();
        if (mapStack == null) {
            Stack> stack = new Stack<>();
            variableMapStack.set(stack);
        }
        variableMapStack.get().push(Maps.newHashMap());

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy