开发者

Tomcat Log4J problem - no appenders could be found

We have a very simple log4j setup for tomcat. We have only a single log4j.properties file and have added the debug flag at startup to verify that the correct log4j.properties file is being used.

Here is our configuration:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %.20t:%c{1}:%L - %m%n
log4j.appender.stdout2=org.apache.log4j.ConsoleAppender
log4j.appender.stdout2.Target=System.out
log4j.appender.stdout2.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout2.layout.ConversionPattern=%d{ABSOLUTE} %5p %.20t:%c{1}:%L - 开发者_C百科%m%n

log4j.rootLogger=INFO, stdout
log4j.logger.com.rei.framework.model.Table=INFO, stdout2
log4j.logger.com.rei.framework.view.SqlGridDisplayGroup=WARN, stdout2

The problem we are having is that unless I specifically list a logger for each class I get this error:

log4j:WARN No appenders could be found for logger (com.rei.util.DatabaseConnection).
log4j:WARN Please initialize the log4j system properly.

According to the docs the ROOT logger should be the default logger for any classes without a specific configuration line, yet I get many "No appenders found" warnings if I don't enumerate each class in the configuration. With hundreds of classes in our project this is not maintainable.

We instantiate the logger this way inside each class:

static Logger logger = org.apache.log4j.Logger.getLogger(Myclass.class);

Any ideas why the root logger in this configuration is not being picked up for undefined classes?


You are right, log4j.rootLogger should be the default configuration. The behaviour that you're getting happens when you haven't got the line

log4j.rootLogger=INFO, stdout

in your file. I've tested with the above file, and this works for me. Therefore, in the file you're actually using, you don't have that line, or you're using a different version of log4j from me (I've tried with 1.2.9 to 1.2.16).

When you specify the -Dlog4j.debug on the command line, you should get output like:

log4j: Using URL [file:/C:/developpement/mjf-workspace/Stackoverflow/lib/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/C:/developpement/mjf-workspace/Stackoverflow/lib/log4j.properties
log4j: Could not find root logger information. Is this OK?
log4j: Parsing for [com.rei.framework.model.Table] with value=[INFO, stdout2].
log4j: Level token is [INFO].
log4j: Category com.rei.framework.model.Table set to INFO

Do you get the line:

log4j: Could not find root logger information. Is this OK?

If you are getting that, then there is something wrong with your log4j.properties file. This is the java class I'm using to test:

package uk.co.farwell.stackoverflow;

import org.apache.log4j.*;

public class Log4jTest {
    static Logger logger = Logger.getLogger(Log4jTest.class);

    public static void main(String args[]) {
        logger.debug("hello Log4jTest");
    }
}

EDIT: It turns out that the Logger class that you're importing into the class and thus using is the Apache commons logging, not log4j. Apache commons logging uses rootCategory, not rootLogger (see Configuring Log4J:

log4j.rootCategory=INFO, stdout

This is why it's not picking up the default logger. Try adding this line to your log4j.properties.

It's not a good idea to mix your logging frameworks, so pick one and stick with it. If commons logging works for you, use that. But don't mix and match in your code. It's a bad idea, and leads to a lot of confusion.

Personally, I always use slf4j. slf4j allows you to have one single configuration for multiple frameworks, so I use that by default. slf4j is often mandatory, because third party libraries use commons logging or even java logging, and you can't change this. slf4j uses a bridge so that everything turns up in the same place, and works too.

It also has a number of advtanges, such as parameterized logging.

Recommendation: use slf4j.


Make sure your log4j.properties is in the classpath!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜