我第一次在项目中使用log4j,我试图为日志输出使用自定义配置。我希望记录器配置位于自定义文件中,而不是log4j.xml或log4j.properties。这就是我现在所拥有的:log4j - 没有appender可以找到,自定义配置文件
构造:
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 [email protected]
log4j: Trying to find [log4j.xml] using [email protected] class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader [email protected]
log4j: Trying to find [log4j.properties] using [email protected] 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 [email protected]
log4j: Trying to find [log4j.xml] using [email protected] class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader [email protected]
log4j: Trying to find [log4j.properties] using [email protected] 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及其附加器A1,而不是rootLogger和A0?我会期待getLogger("testLogger1")
这样做。