我第一次在項目中使用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")
這樣做。