我已经把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.

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


当前回答

这个网站上的解决方案为我工作https://crunchify.com/java-how-to-configure-log4j-logger-property-correctly/。现在我在log4j中没有看到任何警告

我把它放在log4j中。我把它放在src/main/resources中

# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, theConsoleAppender

# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

其他回答

我在使用log4j2时也遇到了同样的问题。我的问题是由使用错误的依赖库引起的:

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <scope>runtime</scope>
    </dependency>

相反,我应该使用:

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <scope>runtime</scope>
    </dependency>

在我的情况下,我有一个log4j2.xml定义在我的“资源”目录,并指定使用它:

System.setProperty("log4j.configurationFile", "log4j2.xml");

如前所述,有两种方法

首先是将这一行添加到你的main方法中:

BasicConfigurator.configure();

第二种方法是添加这个标准log4j。属性文件到你的类路径:

在采用第二种方法时,你需要确保正确地初始化了文件, 如。

Properties props = new Properties();
props.load(new FileInputStream("log4j property file path"));
props.setProperty("log4j.appender.File.File", "Folder where you want to store log files/" + "File Name");

确保创建了存储日志文件所需的文件夹。

这个网站上的解决方案为我工作https://crunchify.com/java-how-to-configure-log4j-logger-property-correctly/。现在我在log4j中没有看到任何警告

我把它放在log4j中。我把它放在src/main/resources中

# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, theConsoleAppender

# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

如果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...
}

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

我的身材里只有这个。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'

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