设置一个新的独立应用程序并希望使用 log4j。我在 Netbeans (6.5) 上使用 Maven,并且能够添加 log4j jar/依赖项,但它似乎找不到我的 log4j.xml 文件,我不知道如何告诉 Netbeans 找到它。
这是我的错误:
log4j:WARN No appenders could be found for logger (com.domain.project).
log4j:WARN Please initialize the log4j system properly.
log4j.xml 路径是 src/main/resources/com/domain/product/gui/resource/log4j.xml
注意:选择 log4j.xml 的位置是为了与遗留代码保持一致(即这样未来的维护者可以找到它),但如果需要,可以更改。
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.domain.project</groupId>
<artifactId>project-log</artifactId>
<packaging>jar</packaging>
<version>0.1-SNAPSHOT</version>
<name>project-log</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
这是我非常简单的 log4j.xml:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<category name="com.domain.project">
<priority value="debug"/>
</category>
<root>
<priority value="info"/>
<appender-ref ref="ConsoleAppender"/>
</root>
</log4j:configuration>
我呼吁设置日志记录:
static Logger log = Logger.getLogger("com.domain.project");
我如何告诉 Netbeans 为这个配置文件设置一个 Maven 类路径?
伊莱恩