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 + "].");
}
}
}