我是第一次在项目中使用log4j,我尝试使用自定义配置进行日志输出。我希望记录器配置在自定义文件中,而不是log4j.xml或log4j.properties。这就是我现在所拥有的:
构造
public Manager(int managerID, String loggerConfigFile) {
this.MANAGER_ID = managerID;
logger = Logger.getLogger("testLogger" + MANAGER_ID);
PropertyConfigurator.configure(loggerConfigFile);
}
第一次调用记录器的方法:
public void getPacketsFromStream(InputStream inputStream) {
logger.info("Manager " + MANAGER_ID + " started.");
(还有更晚,但这并不重要)
test1.config的内容(这是构造函数中loggerConfigFile的值)
testLogger1=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
我已经检查过,并且类路径中的配置文件是。
我希望这会导致记录器将一个语句写入控制台。相反,我得到(使用-Dlog4j.debug标志)以下输出:
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@1f12c4e.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@1f12c4e class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@1f12c4e.
log4j: Trying to find [log4j.properties] using sun.misc.Launcher$AppClassLoader@1f12c4e class loader.
log4j: Trying to find [log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [null].
log4j: Could not find root logger information. Is this OK?
log4j: Finished configuring.
log4j:WARN No appenders could be found for logger (testLogger1).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
我做错了什么?
编辑:
我做了第一个回答建议 - 之前移动了log4j配置并将rootLogger添加到配置文件log4j.rootLogger=DEBUG,A1
,并获得了预期的输出。然后我尝试更多地修改配置文件,最后得到了这个:
log4j.rootLogger=DEBUG,A0
testLogger1=DEBUG,A1
log4j.appender.A0=org.apache.log4j.ConsoleAppender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A0.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A0.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x --- %m%n
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
请注意,A0和A1的输出格式略有不同。 这就是我在输出中得到的结果:
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@1f12c4e.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@1f12c4e class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@1f12c4e.
log4j: Trying to find [log4j.properties] using sun.misc.Launcher$AppClassLoader@1f12c4e class loader.
log4j: Trying to find [log4j.properties] using ClassLoader.getSystemResource().
log4j: Could not find resource: [null].
log4j: Parsing for [root] with value=[DEBUG,A0].
log4j: Level token is [DEBUG].
log4j: Category root set to DEBUG
log4j: Parsing appender named "A0".
log4j: Parsing layout options for "A0".
log4j: Setting property [conversionPattern] to [%-4r %-5p [%t] %37c %3x --- %m%n].
log4j: End of parsing for "A0".
log4j: Parsed "A0" options.
log4j: Finished configuring.
0 INFO [main] testLogger1 --- Manager 1 started.
如何让log4j使用testLogger1及其appender A1,而不是rootLogger和A0?我希望getLogger("testLogger1")
能够做到这一点。
答案 0 :(得分:1)
您应该在实例化Logger之前配置Log4J :
public Manager(int managerID, String loggerConfigFile) {
PropertyConfigurator.configure(loggerConfigFile);
this.MANAGER_ID = managerID;
logger = Logger.getLogger("testLogger" + MANAGER_ID);
}
您还应该在配置中定义根记录器以避免"无法找到根记录器信息&#34 ;;例如:
log4j.rootLogger=DEBUG, A1
有关详细信息,请参阅http://logging.apache.org/log4j/1.2/manual.html。例如,您可以使用Java环境变量来指定包含Log4J配置的文件名/路径(而不是在Java代码中执行此操作)。
答案 1 :(得分:0)
阅读完整的log4j手册似乎已经奏效 - 似乎在配置文件中,记录器名称必须以log4j.logger.
为前缀。以下配置文件对我有用:
log4j.logger.testLogger1=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n