根据Apache Log4j FAQ页面:
Why do I see a warning about "No appenders found for logger" and "Please configure log4j properly"?
This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use. log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments.
基本上,警告No appenders could be found for logger意味着您正在使用log4j日志记录系统,但是您还没有在配置文件中添加任何appenders(例如FileAppender, ConsoleAppender, SocketAppender, SyslogAppender等),或者配置文件丢失。
配置log4j有三种方法:使用属性文件(log4j.properties)、使用XML文件和通过Java代码(rootLogger. properties)。新NullAppender addAppender(());。
log4j . properties
如果你有属性文件(例如安装Solr时),你需要把这个文件放在你的类路径目录中。
类路径
下面是Linux中如何确定类路径值的一些命令建议:
$ echo $CLASSPATH
$ ps wuax | grep -i classpath
$ grep -Ri classpath /etc/tomcat? /var/lib/tomcat?/conf /usr/share/tomcat?
或者从Java: System.getProperty(" Java .class.path")。
Log4j XML
下面是log4j的XML格式的基本XML配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
Tomcat
如果您正在使用Tomcat,您可以放置您的log4j。属性到/usr/share/tomcat?/lib/ /var/lib/tomcat?/ webapps / * / web - inf / lib文件夹中。
Solr
作为参考,Solr default log4j。属性文件如下所示:
# Logging level
solr.log=logs/
log4j.rootLogger=INFO, file, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n
#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9
#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n
log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN
# set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF
为什么log4j不能在J2EE或WAR应用程序中找到我的属性文件?
简单的回答是:log4j类和属性文件不在同一个类加载器的范围内。
Log4j只使用默认的Class.forName()机制来加载类。资源的处理类似。有关更多细节,请参阅java.lang.ClassLoader的文档。
因此,如果遇到问题,请尝试自己加载类或资源。如果您找不到它,那么log4j也找不到。;)
参见:
Apache站点上log4j的简短介绍
Apache:日志服务:Apache站点的常见问题