我已经把log4j放到了我的buildpath中,但是当我运行我的应用程序时,我得到了以下消息:

log4j:WARN No appenders could be found for logger (dao.hsqlmanager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

这些警告是什么意思?这里的阑尾是什么?


当前回答

如果log4j。你正在使用Spring Boot创建一个WAR文件部署到应用服务器上,你省略了一个web.xml文件来支持Spring Boot的自动配置,并且你没有得到任何日志消息,你需要显式地配置Log4j。假设您正在使用Log4j 1.2.x:

public class AppConfig extends SpringBootServletInitializer {

    public static void main( String[] args ) {
        // Launch the application
        ConfigurableApplicationContext context = SpringApplication.run( AppConfig.class, args );
    }

    @Override
    protected SpringApplicationBuilder configure( SpringApplicationBuilder application ) {
        InputStream log4j = this.getClass().getClassLoader().getResourceAsStream("log4j.properties");
        PropertyConfigurator.configure(log4j);
        return application;
    }

// Other beans as required...
}

其他回答

当Log4J Java代码在程序中搜索以创建第一行日志时,Log4J将显示此警告消息。

此时Log4j做了两件事

它搜索log4j。属性文件 它搜索实例化log4j.properties中定义的appender

如果log4J没有找到log4J。属性文件或在log4j中声明的appender。在log4j的其他地方没有定义Rootlogger。系统弹出“属性”文件中的警告信息。

注意:“属性”文件内容必须正确。

以下内容不正确

log4j.rootLogger=file

log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=c:/Trace/MsgStackLogging.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%m%n
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=false

因为文件appender在log4j中是以小写形式声明的。并在log4j中定义。使用大写!

正确的文件应该是

log4j.rootLogger=FILE

log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=c:/Trace/MsgStackLogging.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%m%n
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=false

如果使用MAVEN,则必须放置log4j。src/main/resources中的properties文件并启动MAVEN构建。

Log4j。然后将属性文件复制到目标/类文件夹中。

Log4J使用Log4J。属性文件,它发现在目标/类!

对我来说,原因显然不同,错误消息具有误导性。

我的身材里只有这个。Gradle,它会抱怨在日志的开始缺少slf4j,但仍然会记录东西,尽管格式很差:

    compile 'log4j:log4j:1.2.17'

添加这个依赖会导致前面讨论过的“找不到追加器”错误消息,尽管我已经在src/main/java/log4j.properties中定义了它们:

    compile 'log4j:log4j:1.2.17'
    compile 'org.slf4j:slf4j-log4j12:1.7.25'

最后,添加以下依赖(我只是通过从另一个项目中复制它来猜测)解决了这个问题:

    compile 'log4j:log4j:1.2.17'
    compile 'org.slf4j:slf4j-log4j12:1.7.25'
    compile 'commons-logging:commons-logging:1.2'

我不知道为什么,但用这个就行了。对此有什么见解吗?

得到同样的错误。下面是导致错误信息的问题:

在配置log4j之前,我创建了一些使用Logger的对象:

Logger.getLogger(Lang.class.getName()).debug("Loading language: " + filename);

解决方案: 在main方法的开头配置log4j:

PropertyConfigurator.configure(xmlLog4JConfigFile); 
// or BasicConfigurator.configure(); if you dont have a config file

当我试图运行单元测试时,在IntelliJ中得到这个错误消息。我同时打开了Eclipse和IntelliJ,这可能导致了这个问题。在我关闭两个IDE并运行maven命令行构建后,错误就消失了。

我在尝试用intellij 12中的maven构建可执行jar时遇到了这个问题。结果是,因为java清单文件不包括类路径,所以无法在根级别(jar文件从那里执行)找到log4j属性文件。

供参考,我是这样得到记录器的:

Logger log = LogManager.getLogger(MyClassIWantedToLogFrom.class);

我能够让它工作与一个pom文件,其中包括:

         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-5</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath> 
                        <mainClass>com.mycompany.mainPackage.mainClass</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>.</Class-Path> <!-- need to add current directory to classpath properties files can be found -->
                    </manifestEntries>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>