zoukankan      html  css  js  c++  java
  • 日志工具类

    package com.pt.platform.core.common;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class JYLoggerUtil {
        private static final Logger log = LoggerFactory.getLogger(JYLoggerUtil.class);
        private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        private static ThreadLocal<Map<String, Date>> threadMap = new ThreadLocal();
    
        public JYLoggerUtil() {
        }
    
        public static <T> void info(Class<T> clazz, String message) {
            Logger logger = LoggerFactory.getLogger(clazz);
            logger.info(message);
        }
    
        public static <T> void info(Class<T> clazz, String message, Throwable t) {
            Logger logger = LoggerFactory.getLogger(clazz);
            logger.info(message, t);
        }
    
        public static <T> void debug(Class<T> clazz, String message) {
            Logger logger = LoggerFactory.getLogger(clazz);
            logger.debug(message);
        }
    
        public static <T> void error(Class<T> clazz, String message) {
            Logger logger = LoggerFactory.getLogger(clazz);
            logger.error(message);
        }
    
        public static <T> void error(Class<T> clazz, String message, Exception ex) {
            Logger logger = LoggerFactory.getLogger(clazz);
            logger.error(message, ex);
        }
    
        public static <T> void logCurrentTime(String key, boolean isStart, Class<T> cls) {
            if(log.isInfoEnabled()) {
                Object timeMap = (Map)threadMap.get();
                if(timeMap == null) {
                    timeMap = new HashMap();
                    threadMap.set(timeMap);
                }
    
                String mapKey = cls.getName() + ":" + key;
                Date curDate = new Date();
                if(isStart) {
                    ((Map)timeMap).put(mapKey, curDate);
                } else {
                    Date lastDate = (Date)((Map)timeMap).get(mapKey);
                    if(lastDate != null) {
                        ((Map)timeMap).remove(mapKey);
                        StringBuilder logInfo = new StringBuilder("=========");
                        logInfo.append(",(位置:").append(cls.getName());
                        logInfo.append(",内容:").append(key).append(")");
                        logInfo.append(",间隔:").append(curDate.getTime() - lastDate.getTime());
                        logInfo.append(",开始start:").append(sdf.format(lastDate));
                        logInfo.append(",结束end:").append(sdf.format(curDate)).append("=========");
                        log.info(logInfo.toString());
                        threadMap.remove();
                    }
                }
    
            }
        }
    }
  • 相关阅读:
    Java内部类详解
    Mac连接linux服务器
    mac 添加环境变量(jmeter添加至环境变量中)
    Charles使用断点修改返回或请求参数
    解决git clone速度慢的问题
    group by分组后获得每组中时间最大的那条记录
    使用Charles进行弱网测试
    http常见的状态码
    Mac使用Charles抓取ios手机APP中的https请求
    airtest使用poco方法的断言知识
  • 原文地址:https://www.cnblogs.com/yy123/p/7388255.html
Copyright © 2011-2022 走看看