[英]log4j - no appenders could be found, custom config file
I'm using log4j in a project for the first time, and I'm trying to use a custom configuration for the log output. 我第一次在项目中使用log4j,并且尝试对日志输出使用自定义配置。 I want the logger configuration to be in a custom file, not log4j.xml or log4j.properties.
我希望记录器配置位于自定义文件中,而不是log4j.xml或log4j.properties中。 This is what I have right now:
这就是我现在所拥有的:
Constructor: 构造函数:
public Manager(int managerID, String loggerConfigFile) {
this.MANAGER_ID = managerID;
logger = Logger.getLogger("testLogger" + MANAGER_ID);
PropertyConfigurator.configure(loggerConfigFile);
}
Method that calls logger for the first time: 首次调用记录器的方法:
public void getPacketsFromStream(InputStream inputStream) {
logger.info("Manager " + MANAGER_ID + " started.");
(there's more later, but that's not important) (还有更多,但这并不重要)
Contents of test1.config (that's the value of loggerConfigFile in the constructor) 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
I've checked, and the config file is in the classpath. 我检查,并配置文件在类路径中。
I'd expect this to result in the logger writing that one statement to the console. 我希望这会导致记录器将那一条语句写入控制台。 Instead, I get (with the -Dlog4j.debug flag used) the following output:
相反,我得到(使用-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.
What am I doing wrong? 我究竟做错了什么?
Edit: 编辑:
I did as the first answer suggests - moved the log4j configuration before and added rootLogger to the configuration file log4j.rootLogger=DEBUG,A1
, and got the expected output. 我按照第一个答案的建议进行操作-之前移动了log4j配置,并将rootLogger添加到配置文件
log4j.rootLogger=DEBUG,A1
,并获得了预期的输出。 Then I tried to modify the config file a bit more, and ended up with this: 然后,我尝试对配置文件进行更多修改,并最终得到以下结果:
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
Note that A0 and A1 have slightly different output formats. 请注意,A0和A1的输出格式略有不同。 This is what I got in the output:
这是我在输出中得到的:
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.
How can I get log4j to use testLogger1 and its appender A1, rather than rootLogger and A0? 如何获取log4j以使用testLogger1及其附加程序A1,而不是rootLogger和A0? I would've expected
getLogger("testLogger1")
to do that. 我希望
getLogger("testLogger1")
能够做到这一点。
You should configure Log4J before instantiating a Logger: 您应在实例化Logger 之前配置Log4J:
public Manager(int managerID, String loggerConfigFile) {
PropertyConfigurator.configure(loggerConfigFile);
this.MANAGER_ID = managerID;
logger = Logger.getLogger("testLogger" + MANAGER_ID);
}
You should also define a root logger in your configuration to avoid "Could not find root logger information"; 您还应该在配置中定义一个根记录器,以避免“找不到根记录器信息”。 for example:
例如:
log4j.rootLogger=DEBUG, A1
See http://logging.apache.org/log4j/1.2/manual.html for more details. 有关更多详细信息,请参见http://logging.apache.org/log4j/1.2/manual.html 。 For example you could use a Java environment variable to specify the file name/path that contains Log4J configuration (instead of doing this in Java code).
例如,您可以使用Java环境变量来指定包含Log4J配置的文件名/路径(而不是在Java代码中执行此操作)。
Reading the complete log4j manual seems to have worked - it appears that in the config file the logger names must be prefixed with log4j.logger.
阅读完整的log4j手册似乎很有用-似乎在配置文件中,记录器名称必须以
log4j.logger.
为前缀log4j.logger.
. 。 The following config file is working for me:
以下配置文件为我工作:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.