首页  

日志框架关键组件     所属分类 architecture 浏览量 725
Log4j Log4j2 Logback

LoggingEvent Appender Level 
Logger  LoggerFactory
日志层级 Hierarchy
日志上下文  LoggerContext

LoggingEvent
日志时间戳
线程信息
日志名称(一般是全类名)
日志级别
日志内容

Appender
标准输出/控制台(Standard Output/Console) 文件(File) 消息队列(MQ) 数据库 

public interface Appender {
    void append(LoggingEvent event);
}

public class ConsoleAppender implements Appender {
    private OutputStream out = System.out;
    private OutputStream out_err = System.err;

    @Override
    public void append(LoggingEvent event) {
        try {
            out.write(event.toString().getBytes(encoding));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


日志级别  Level
ERROR  WARN  INFO  DEBUG  TRACE

public interface Logger{
    void trace(String msg);
    void info(String msg);
    void debug(String msg);
    void warn(String msg);
    void error(String msg);
    String getName();
}


日志层级  Hierarchy
层级结构,每一个Logger拥有一个Parent Logger
在filterAndLog时优先使用自己的Appender
如果自己没有Appender,向上调用父appnder

Root Logger 全局默认的Logger,是所有Logger(新创建的)的Parent Logger
在filterAndLog时,默认使用Root Logger的appender和level来进行输出



private LogcLogger parent; 

private void filterAndLog(Level level,String msg){
    LoggingEvent e = new LoggingEvent(level, msg,getName());
    // 查找可用的logger进行输出
    for (LogcLogger l = this;l != null;l = l.parent){
        if(l.appender == null){
            continue;
        }
        if(level.toInt() > effectiveLevelInt){
            l.appender.append(e);
        }
        break;
    }
}


public class LoggerContext {
    private Logger root;
    private Map< String,Logger> loggerCache = new HashMap< >();

    public void addLogger(String name,Logger logger){
        loggerCache.put(name,logger);
    }

    public void addLogger(Logger logger){
        loggerCache.put(logger.getName(),logger);
    }
    //getters and setters...
}


public interface ILoggerFactory {
    Logger getLogger(Class< ?> clazz);
    Logger getLogger(String name);
    Logger newLogger(String name);
}

public class StaticLoggerFactory implements ILoggerFactory {

    private LoggerContext loggerContext; 

    @Override
    public Logger getLogger(Class< ?> clazz) {
        return getLogger(clazz.getName());
    }

    @Override
    public Logger getLogger(String name) {
        Logger logger = loggerContext.getLoggerCache().get(name);
        if(logger == null){
            logger = newLogger(name);
        }
        return logger;
    }
    
 
    @Override
    public Logger newLogger(String name) {
        LogcLogger logger = new LogcLogger();
        logger.setName(name);
        Logger parent = null;
        // 拆分包名,向上查找parent logger
        for (int i = name.lastIndexOf("."); i >= 0; i = name.lastIndexOf(".",i-1)) {
            String parentName = name.substring(0,i);
            parent = loggerContext.getLoggerCache().get(parentName);
            if(parent != null){
                break;
            }
        }
        if(parent == null){
            parent = loggerContext.getRoot();
        }
        logger.setParent(parent);
        logger.setLoggerContext(loggerContext);
        return logger;
    }
}

上一篇     下一篇
中国简史

网络流行语

linux 获取进程工作目录

Log4j2 lookup 安全漏洞

nginx常用优化

JFR实战