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

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


当前回答

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

其他回答

我认为您应该理解log4j jar文件或Java代码在哪里查找log4j配置文件。

src / main /资源/ log4j。properties为Eclipse路径。 将它们放置在适当的位置,这样您就不必在代码中硬编码绝对路径。

请阅读我的文章和示例解决方案 https://askyourquestions.info/how-to-see-where-the-log-is-logger-in-slf4j/

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

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

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

错误消息将不再出现。

这个简短的log4j介绍指南有点旧,但仍然有效。

该指南将为您提供一些关于如何使用记录器和附加程序的信息。


你可以采用两种简单的方法。

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

BasicConfigurator.configure();

第二种方法是添加这个标准log4j。属性(从上面提到的指南)文件到你的类路径:

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

添加如下代码作为第一个代码:

Properties prop = new Properties();
prop.setProperty("log4j.rootLogger", "WARN");
PropertyConfigurator.configure(prop);

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

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

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