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

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


当前回答

快速的解决方案:

为main函数添加代码: String log4jConfPath = "/path/to/log4j.properties"; PropertyConfigurator.configure (log4jConfPath); 创建一个名为log4j的文件。/path/to的属性 log4j。rootLogger = INFO, stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = system . out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout。ConversionPattern=%d{yy/MM/dd HH: MM:ss} %p %c{2}: %m%n

其他回答

我在尝试用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 JVM参数Dlog4j.configuration

一般来说:

添加指向log4j配置文件的JVM参数。 语法如下:

java [ options ] -jar file.jar [ arguments ]

一个真实的命令行示例如下所示:

java -Dlog4j.configuration=conf/log4j.xml -jar myJarFile.jar myArg1 myArg2

对于IntelliJ IDE用户:

1.Run/Debug Configurations
2.Edit configurations...
3.VM options
4.Enter the same value also starting with "-D"

小贴士:

1.Eclipse IDE用户将找到等效的方法

2.对于运行/调试配置编辑器,很可能在开始时,您的特定可执行文件不存在。根据您当前正在处理的项目的大小,在目录中找到它可能会令人不快。如果在继续运行/调试配置之前只运行/执行一次文件(单击play),无论执行结果是什么,都不会太麻烦。

3.请注意您的工作目录、相对路径和类路径。

当我试图运行JUnit测试类时,我也遇到了同样的问题。

在手动添加log4j后,问题得到了解决。src/test/resources文件夹中的Properties文件。

将下面的代码添加到log4j。属性文件解决了这个问题:

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

首先导入:

 import org.apache.log4j.PropertyConfigurator;

然后将以下代码添加到main方法:

String log4jConfPath ="path to/log4j.properties";
PropertyConfigurator.configure(log4jConfPath);

创建一个路径为的文件,并将以下代码添加到该文件中。

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n

另一个可能发生这种情况的原因(在RCP4中)是您在目标文件中使用了多个日志记录框架。例如,如果您使用slf4j、log4j和ch.qos.logback的组合,就会发生这种情况。Slf4j的目标文件内容选项卡。