LogHelper.java 5.54 KB
package com.dili.titan.common.log;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Describe: User: BjYangKai Date: 2011-4-21 Time: 12:00:33
 */
public class LogHelper {

    /**
     * stock相关重要日志
     */
    private static Map<LogTypeEnum, Logger> logMap = new HashMap<LogTypeEnum, Logger>();

    static {
        for (LogTypeEnum ft : LogTypeEnum.values()) {
            logMap.put(ft, LoggerFactory.getLogger(ft.getKey()));
        }
    }

    /**
     * 
     * @param str
     * @return
     */
    public static String addLogbusinessId(String str) {
        if (StringUtils.isEmpty(str)) {
            return str;
        } else {
            return "业务标示ID[" + LogBusinessIdUtils.getCurBusinessId() + "],"
                    + str;
        }
    }

    /**
     * 使用默认的文件记录debug信息
     * 
     * @param str 日志信息
     */
    public static void debug(String str, Object... args) {
        debug(LogTypeEnum.DEFAULT, str, args);
    }

    /**
     * 使用默认的文件记录info信息
     * 
     * @param str 日志信息
     */
    public static void info(String str, Object... args) {
        info(LogTypeEnum.DEFAULT, str, args);
    }

    /**
     * 使用默认的文件记录warn信息
     * 
     * @param str 日志信息
     */
    public static void warn(String str, Object... args) {
        warn(LogTypeEnum.DEFAULT, str, args);
    }

    /**
     * 使用默认的文件记录error信息
     * 
     * @param str 日志信息
     */
    public static void error(Throwable e, String str, Object... args) {
        error(LogTypeEnum.DEFAULT, e, str, args);
    }

    /**
     * 记录运行期错误信息
     * 
     * @param e
     */
    public static void execption(Throwable e) {
        error(LogTypeEnum.EXCEPTION, getExceptionTrace(e));
    }

    /**
     * 打印错误信息
     * 
     * @param e
     * @return
     */
    public static String getExceptionTrace(Throwable e) {
        if (e != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            return sw.toString();
        } else
            return null;
    }

    public static Logger getLog(LogTypeEnum type) {
        if (logMap.get(type) == null) {
            return LoggerFactory.getLogger(LogHelper.class);
        } else {
            return logMap.get(type);
        }
    }

    /**
     * 日志消息占位符形式的替换,按照传入参数依次替换"{}"
     * 
     * @param str 带占位符的日志消息
     * @param args 需要被替换的参数,除了字符串和基本类型的对象形式外,其他类型需要自己实现toString方法
     * @return
     */
    public static <T extends Object> String replace(String str, T... args) {
        if (str == null) {
            return addLogbusinessId(null);
        }
        if (args == null || args.length < 1) {
            return addLogbusinessId(str.replaceAll("\\{\\}", " null"));
        }
        StringBuilder builder = new StringBuilder(str);
        int start = builder.indexOf("{");
        int end = builder.indexOf("}", start);
        for (Object arg : args) {
            if (start == -1) {
                break;
            }
            if (arg == null) {
                arg = "null";
            }
            builder.replace(start, end + 1, arg.toString());
            start = start + arg.toString().length();
            start = builder.indexOf("{", start);
            end = builder.indexOf("}", start);
        }
        return addLogbusinessId(builder.toString());
    }

    /**
     * 记录DEBUG信息
     * 
     * @param type 日志业务类型
     * @param str
     */
    public static void debug(LogTypeEnum type, String str, Object... args) {
        Logger log = getLog(type);
        if (log.isDebugEnabled()) {
            String msg = replace(str, args);
            log.debug(msg);
        }
    }

    /**
     * 记录INFO信息
     * 
     * @param type 日志业务类型
     * @param str
     */
    public static void info(LogTypeEnum type, String str, Object... args) {
        Logger log = getLog(type);
        if (log.isInfoEnabled()) {
            String msg = replace(str, args);
            log.info(msg);
        }
    }

    /**
     * 记录warn信息
     * 
     * @param type 日志业务类型
     * @param str
     */
    public static void warn(LogTypeEnum type, String str, Object... args) {
        Logger log = getLog(type);
        if (log.isWarnEnabled()) {
            String msg = replace(str, args);
            log.warn(msg);
        }
    }

    /**
     * 记录error信息
     * 
     * @param type 日志业务类型
     * @param str
     */
    public static void error(LogTypeEnum type, String str, Object... args) {
        Logger log = getLog(type);
        if (log.isErrorEnabled()) {
            String msg = replace(str, args);
            log.error(msg);
        }
    }

    /**
     * 记录error信息
     * 
     * @param type 日志业务类型
     * @param str
     */
    public static void error(LogTypeEnum type, Throwable e, String str,
            Object... args) {
        Logger log = getLog(type);
        if (log.isErrorEnabled()) {
            String msg = replace(str, args);
            log.error(msg, e);
        }
    }

    public static void main(String[] args) {
        LogHelper.debug(LogTypeEnum.DEFAULT, "aaaa");
        System.out.println();
    }
}