欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot中怎样基于slf4j封装日志类输出日志

程序员文章站 2022-07-03 15:30:41
...

场景

slf4jGithub:

https://github.com/qos-ch/slf4j

实现

新建log包,包下新建Log.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class Log {
    private static String logPrefix = "badao log -> ";
    private static Log instance;
    private static Logger logger = null;
    private static Map<Class, Logger> loggerList = new HashMap<Class, Logger>(); //用于缓存logger对象

    /**
     * 定义私有构造方法实现单例
     */
    private Log() {
    }

    /**
     * 功能说明:获取服务实例的静态方法
     * 修改说明:
     * @author badao
     * @date
     * @param obj 传入调用此方法的对象
     * @return
     */
    public synchronized static Log getInst(Object obj) {
        if (instance == null) {
            instance = new Log();
        }
        Log.logger = loggerList.get(obj.getClass());
        if (Log.logger == null) {
            Log.logger = LoggerFactory.getLogger(obj.getClass());
            //Log.logger = Logger.getLogger(obj.getClass());
            loggerList.put(obj.getClass(), Log.logger);
        }
        return instance;
    }

    /**
     * 功能说明:获取服务实例的静态方法
     * 修改说明:
     * @author badao
     * @date
     * @param clazz 传入调用此方法的类型
     * @return
     */
    public synchronized static Log getInst(Class clazz) {
        if (instance == null) {
            instance = new Log();
        }
        Log.logger = loggerList.get(clazz);
        if (Log.logger == null) {
            Log.logger = LoggerFactory.getLogger(clazz);
            loggerList.put(clazz, Log.logger);
        }
        return instance;
    }

    /**
     * 功能说明:获取服务实例的静态方法
     * 修改说明:
     * @author badao
     * @date
     * @return
     */
    public synchronized static Log getInst() {
        if (instance == null) {
            instance = new Log();
        }
        Log.logger = loggerList.get(Log.class);
        if (Log.logger == null) {
            Log.logger = LoggerFactory.getLogger(Log.class);
            loggerList.put(Log.class, Log.logger);
        }
        return instance;
    }

    public void trace(String message) {
        Log.logger.trace(logPrefix + message);
    }

    public void trace(String message, Throwable t) {
        Log.logger.trace(logPrefix + message, t);
    }

    public void debug(String message) {
        Log.logger.debug(logPrefix + message);
    }

    public void debug(String message, Throwable t) {
        Log.logger.debug(logPrefix + message, t);
    }

    public void info(String message) {
        Log.logger.info(logPrefix + message);
    }

    public void info(String message, Throwable t) {
        Log.logger.info(logPrefix + message, t);
    }

    public void warn(String message) {
        Log.logger.warn(logPrefix + message);
    }

    public void warn(String message, Throwable t) {
        Log.logger.warn(logPrefix + message, t);
    }

    public void error(String message,Object... arguments) {
        Log.logger.error(logPrefix + message,arguments);
    }

    public void error(String message, Throwable t, Object... arguments) {
        Log.logger.error(logPrefix + message,t,arguments);
    }
}

使用的地方导入包

import com.badao.log.Log;

然后使用

 try {

            Log.getInst(this).debug("霸道测试成功!");
        } catch (Exception e) {
            Log.getInst(this).debug("霸道测试错误!"+e.getMessage());
        }

 

相关标签: slf4j