简体   繁体   中英

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. I want the logger configuration to be in a custom file, not log4j.xml or 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)

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:

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. 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. 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? I would've expected getLogger("testLogger1") to do that.

You should configure Log4J before instantiating a Logger:

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. 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).

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. . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM