简体   繁体   English

Tomcat Log4J问题-找不到附加程序

[英]Tomcat Log4J problem - no appenders could be found

We have a very simple log4j setup for tomcat. 我们有一个非常简单的tomcat log4j设置。 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. 我们只有一个log4j.properties文件,并在启动时添加了调试标志,以验证是否使用了正确的log4j.properties文件。

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 - %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. 根据文档,ROOT记录器应该是没有特定配置行的任何类的默认记录器,但是如果我没有枚举配置中的每个类,我会收到许多“找不到附加器”的警告。 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. 没错,log4j.rootLogger应该是默认配置。 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). 因此,在您实际使用的文件中,没有该行,或者您使用的是与我不同的log4j版本(我已经尝试使用1.2.9到1.2.16)。

When you specify the -Dlog4j.debug on the command line, you should get output like: 在命令行上指定-Dlog4j.debug时,应该获得如下输出:

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. 如果您得到了,那么您的log4j.properties文件有问题。 This is the java class I'm using to test: 这是我用来测试的Java类:

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. 编辑:事实证明,您要导入该类并因此使用的Logger类是Apache Commons日志记录,而不是log4j。 Apache commons logging uses rootCategory, not rootLogger (see Configuring Log4J : Apache Commons日志记录使用rootCategory,而不是rootLogger(请参阅配置Log4J

log4j.rootCategory=INFO, stdout

This is why it's not picking up the default logger. 这就是为什么它不使用默认记录器的原因。 Try adding this line to your log4j.properties. 尝试将此行添加到您的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 slf4j allows you to have one single configuration for multiple frameworks, so I use that by default. slf4j允许您对多个框架进行单一配置,因此默认情况下使用该配置。 slf4j is often mandatory, because third party libraries use commons logging or even java logging, and you can't change this. slf4j通常是强制性的,因为第三方库使用commons日志甚至Java日志,并且您不能更改它。 slf4j uses a bridge so that everything turns up in the same place, and works too. slf4j使用网桥,以便所有内容都出现在同一位置,并且也可以工作。

It also has a number of advtanges, such as parameterized logging . 它还具有许多辅助功能,例如参数化日志记录

Recommendation: use slf4j. 建议:使用slf4j。

确保您的log4j.properties在类路径中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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