log4j扩展:独立的Repository

Logger Repository从字面上理解,它是一个Logger的容器,它会创建并缓存Logger实例,从而具有相同名字的Logger实例不会多次创建,以提高性能。

Log4J支持两种配置文件:properties文件和xml文件。Configurator解析配置文件,并将解析后的信息添加到Repository中,log4j本身提供的LogManager最终将Repository和Configurator[/size][size=10pt;]整合在一起。

我们在使用log4j日志组件的时候,有时需要定义自身的Repository,这就需要定义自己的LogManager,去加载我们自身的配置文件。

public final class Log4jManager
{
    private static Object guard = null;

    private static RepositorySelector repositorySelector;

    static
    {
        Hierarchy h = new Hierarchy(new RootLogger((Level) Level.DEBUG));
        repositorySelector = new DefaultRepositorySelector(h);
        doConfigure();
    }

    //其他方法和LogManager类似,可以参考LogManager
    //...

    //加载配置文件
    public static void doConfigure()
    {
        URL url = null;
        

        //LogConstant.LOG_CONGFIG_FILE_PATH即为定义的配置文件名       
        url = Log4jManager.class.getClassLoader().getResource(
            LogConstant.LOG_CONGFIG_FILE_PATH);
        if (url != null)
        {
            try
            {
                OptionConverter.selectAndConfigure(url, null,
                    Log4jManager.getLoggerRepository());
            }
            catch (NoClassDefFoundError e)
            {
                LogLog.warn("Error during default initialization", e);
            }
        }
        else
        {
            LogLog.debug("Could not find resource: ["
                + LogConstant.LOG_CONGFIG_FILE_PATH + "].");
        }

    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值