Log.java 4.21 KB
package com.yanzuoguang.util.log;

import com.yanzuoguang.util.extend.ConfigBase;
import com.yanzuoguang.util.helper.StringHelper;

import java.util.Date;
import java.util.HashMap;

/**
 * 日志操作类
 *
 * @author 颜佐光
 */
public class Log {

    /**
     * 当前线程缓存的对象
     */
    private static final HashMap<String, LogDate> threadCache = new HashMap<>();

    /**
     * 缓存的日志处理对象
     */
    private static final RunnableLog writeLogDefault = new LogDefault();

    /**
     * 写入错误消息
     *
     * @param cls  类
     * @param msg
     * @param args
     */
    public static void error(Class<?> cls, String msg, Object... args) {
        String toMsg = getFormat(msg, args);
        add(new LogInfo(cls, new Date(), true, null, toMsg));
    }

    /**
     * 写入错误信息
     *
     * @param ex
     */
    public static void error(Class<?> cls, Throwable ex) {
        add(new LogInfo(cls, new Date(), ex != null, ex, StringHelper.EMPTY));
    }

    /**
     * 写入错误信息
     *
     * @param ex   错误内容
     * @param msg  错误消息
     * @param args 错误消息
     */
    public static void error(Class<?> cls, Throwable ex, String msg, Object... args) {
        String toMsg = getFormat(msg, args);
        add(new LogInfo(cls, new Date(), ex != null, ex, toMsg));
    }

    /**
     * 警告信息
     *
     * @param msg
     * @param args
     */
    public static void info(Class<?> cls, String msg, Object... args) {
        String toMsg = getFormat(msg, args);
        add(new LogInfo(cls, new Date(), false, null, toMsg));
    }

    /**
     * 警告信息
     *
     * @param msg
     * @param args
     */
    public static void infoTag(Class<?> cls, String tag, String msg, Object... args) {
        String toMsg = getFormat(msg, args);
        LogInfo info = new LogInfo(cls, new Date(), null, toMsg);
        info.setTag(tag);
        add(info);
    }

    /**
     * 将日志添加到处理队列
     *
     * @param info 需要处理的信息
     */
    private static void add(LogInfo info) {
        // 获取当前线程编号
        String threadId = getThreadId();
        info.setThreadId(threadId);

        // 判断当前线程日志是否需要特殊处理
        LogDate date;
        synchronized (Log.class) {
            if (!threadCache.containsKey(threadId)) {
                date = threadBegin();
            } else {
                date = threadCache.get(threadId);
            }
        }
        if (date == null) {
            return;
        }
        date.commit();
        info.setTime((long) date.getLastSecond());
        info.setTotalTime((long) date.getTotalSecond());
        if (ConfigBase.getWriteLog() != null) {
            ConfigBase.getWriteLog().run(info);
        } else {
            writeLogDefault.run(info);
        }
    }

    /**
     * 当前线程特殊处理异常,一旦调用必须调用 threadCommit 函数
     */
    public static LogDate threadBegin() {
        // 获取当前线程编号
        String threadId = getThreadId();
        LogDate logDate = new LogDate();
        logDate.clear();
        threadCache.put(threadId, logDate);
        return logDate;
    }

    /**
     * 当前县城日志对象
     *
     * @return
     */
    public static LogDate threadCurrent() {
        String threadId = getThreadId();
        return threadCache.getOrDefault(threadId, null);
    }

    /**
     * 当前线程结束处理特殊异常
     */
    public static LogDate threadCommit() {
        String threadId = getThreadId();
        LogDate log = threadCurrent();
        threadCache.remove(threadId);
        return log;
    }

    /**
     * 获取当前线程编号
     *
     * @return
     */
    private static String getThreadId() {
        return String.valueOf(Thread.currentThread().getId());
    }

    /**
     * 将异常消息格式化
     *
     * @param msg
     * @param args
     * @return
     */
    private static String getFormat(String msg, Object... args) {
        if (args != null && args.length > 0) {
            try {
                msg = String.format(msg, args);
            } catch (Exception ex) {
                Log.error(Log.class, ex);
            }
        }
        return msg;
    }
}