美文网首页
Logger增强

Logger增强

作者: Jorvi | 来源:发表于2019-06-14 10:20 被阅读0次

    1. 获取Logger

        public static Logger getLogger(String loggerName) {
            // 获取org.slf4j.Logger
            Logger logger = LoggerFactory.getLogger(loggerName);
    
            // 封装自定义Logger
            CustomLogger customLogger = new CustomLogger(logger);
    
            // 返回自定义Logger
            return customLogger;
        }
    

    2. 自定义Logger

    /**
     * 自定义Logger
     */
    public class CustomLogger implements Logger {
       
        // 将org.slf4j.Logger包装起来
        private Logger logger;
    
        public CustomLogger() {
            super();
        }
    
        public CustomLogger(Logger logger) {
            super();
            this.logger = logger;
        }
    
        @Override
        public String getName() {
            return logger.getName();
        }
    
        @Override
        public boolean isTraceEnabled() {
            return logger.isTraceEnabled();
        }
    
        @Override
        public void trace(String s) {
            // 增强Logger
            String enhanceStr = "开始增强了: " + s;
            logger.trace(enhanceStr);
        }
    
        ......
    
    }
    

    相关文章

      网友评论

          本文标题:Logger增强

          本文链接:https://www.haomeiwen.com/subject/kgpgzqtx.html