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

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


当前回答

如果你和很多项目一起工作,你可能会面临一个风格问题。

*你必须有一个lof4j。属性文件,此文件包括其他项目的日志属性。

*此外,您可以尝试将log4j属性文件放入src路径,当项目工作在Linux操作系统,其他项目的库和log4。属性文件可以放在一个文件夹下的类路径上的某个位置。

其他回答

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

我在尝试用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>

在代码中使用Logger记录消息。Appender是附加到Logger的对象,用于将消息写入特定目标。FileAppender用于写入文本文件,ConsoleAppender用于写入控制台。您需要显示Logger和Appender设置的代码以获得更多帮助。

为了更好地理解Logger和Appender的交互,请阅读教程。

我在使用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");

看起来您需要添加log4j的位置。属性文件到Eclipse中的类路径中。

确保您的项目在Eclipse中是打开的,然后单击Eclipse顶部的“Run”菜单,然后单击以下内容:

运行 运行配置 类路径(选项卡) 用户条目 高级(右侧按钮) 添加文件夹 然后导航到包含log4j的文件夹。属性文件 应用 运行

错误消息将不再出现。