Logger增强

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增强)