JFinal - Log 日志

今天偶然发现 JFinal 的 Log 简单小巧。上代码。

 

JFinal 在初始化的时候有初始化 Log。

class Config {
    // ...
    
    static void configJFinal(JFinalConfig jfinalConfig) {
        jfinalConfig.configConstant(constants);                initLogFactory(); // 初始化 Log
        jfinalConfig.configRoute(routes);
        jfinalConfig.configPlugin(plugins);                    startPlugins();    // very important!!!
        jfinalConfig.configInterceptor(interceptors);
        jfinalConfig.configHandler(handlers);
    }
    
    private static void initLogFactory() {
        LogManager.me().init();
        log = Log.getLog(Config.class);
        JFinalFilter.initLog();
    }
}

 

接着 LogManager.me().init()。

public class LogManager {
    
    private static final LogManager me = new LogManager();
    
    private LogManager() {}
    
    public static LogManager me() {
        return me;
    }
    
    public void init() {
        Log.init();
    }
    
    public void setDefaultLogFactory(ILogFactory defaultLogFactory) {
        Log.setDefaultLogFactory(defaultLogFactory);
        com.jfinal.kit.LogKit.synchronizeLog();
    }
}

 

接着 Log.init(),在这里完成 defaultLogFactory 的实例化。当然也可以使用 Log.setDefaultFactory 重新设置 defaultLogFactory。

public abstract class Log {
    
    private static ILogFactory defaultLogFactory = null;
    
    static {
        init();
    }
    
    static void init() {
        if (defaultLogFactory == null) {
            try {
                Class.forName("org.apache.log4j.Logger");
                Class<?> log4jLogFactoryClass = Class.forName("com.jfinal.log.Log4jLogFactory");
                defaultLogFactory = (ILogFactory)log4jLogFactoryClass.newInstance();    // return new Log4jLogFactory();
            } catch (Exception e) {
                defaultLogFactory = new JdkLogFactory();
            }
        }
    }
    
    static void setDefaultLogFactory(ILogFactory defaultLogFactory) {
        if (defaultLogFactory == null) {
            throw new IllegalArgumentException("defaultLogFactory can not be null.");
        }
        Log.defaultLogFactory = defaultLogFactory;
    }
    
    public static Log getLog(Class<?> clazz) {
        return defaultLogFactory.getLog(clazz);
    }
    
    public static Log getLog(String name) {
        return defaultLogFactory.getLog(name);
    }
    
    public abstract void debug(String message);
    public abstract void debug(String message, Throwable t);
    public abstract void info(String message);
    public abstract void info(String message, Throwable t);
    public abstract void warn(String message);
    public abstract void warn(String message, Throwable t);
    public abstract void error(String message);
    public abstract void error(String message, Throwable t);
    public abstract void fatal(String message);
    public abstract void fatal(String message, Throwable t);
    public abstract boolean isDebugEnabled();
    public abstract boolean isInfoEnabled();
    public abstract boolean isWarnEnabled();
    public abstract boolean isErrorEnabled();
    public abstract boolean isFatalEnabled();
}

JFinal 默认使用 log4j 来管理日志,但是如果 JVM 在加载 org.apache.log4j.Logger 或 com.jfinal.log.Log4jLogFactory 的时候出现异常,就会使用 jdk 的日志系统来管理日志了。

 

JFinal 给了两个可以直接利用的 LogFactory:Log4jLogFactory 和 JdkLogFactory,它们分别可以通过 getLog(Class class) 得到 Log4jLog  和 JdkLog  实例。而 Log4jLog 和 JdkLog 分别封装了 org.apache.log4j.Logger 和 java.util.logging.Logger,并且利用它们进行日志操作。可以看出我们还可以使用别的 log 工具来管理日志,只需做到两点:1. 自定义工厂类继承 ILogFactory;2. 自定义 log 类继承 com.jfinal.log 并且对所用 log 工具进行封装。

 

当需要一个 Log 实例的时候,如第一段代码中 - Log.getLog(Class class),接着 - defaultLogFactory.getLog(Class class) new 出一个新的 Log 对象。

public class Log4jLogFactory implements ILogFactory {
    
    public Log getLog(Class<?> clazz) {
        return new Log4jLog(clazz);
    }
    
    public Log getLog(String name) {
        return new Log4jLog(name);
    }
}

 

题外话:这里用到了工厂模式。并且 JFinal 相当多的地方使用到了单例模式,例如第二段代码中的 LogManager。后面会专门总结一下设计模式。

 

转载于:https://www.cnblogs.com/xmsx/p/5581901.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值