如何使用 Netbeans 和 Maven 为独立应用程序配置 log4j
设置一个新的独立应用程序并想要使用 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 类路径?
伊兰
Setting up a new standalone app and want to use log4j. Am using Maven on Netbeans (6.5), and was able to add the log4j jar/dependency, but it can't seem to find my log4j.xml file and I'm not sure how to tell Netbeans to find it.
Here's my error:
log4j:WARN No appenders could be found for logger (com.domain.project).
log4j:WARN Please initialize the log4j system properly.
log4j.xml path is src/main/resources/com/domain/product/gui/resource/log4j.xml
Note: the location of the log4j.xml was chosen to be consistent with legacy code (i.e. so future maintainers can find it), but this could be changed if necessary.
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>
Here's my very simple 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>
And my call to set up logging:
static Logger log = Logger.getLogger("com.domain.project");
How do I tell Netbeans to set up a Maven classpath to this configuration file?
Ilane
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最近,当我想在从 IDE 中启动应用程序时让日志记录正常工作时,我遇到了同样的问题。
我的解决方案是使用 nbm-maven-plugin< 的 additionalArguments /a> 如下图所示。
请注意 -J-Dlog4j.configuration=... 的使用,额外的 -J 确保该选项传递给应用程序,否则它只会影响 maven 启动器的 JVM。
I recently faced the same problem when I wanted to get the logging working when the application was launched from within the IDE.
My solution is to use the additionalArguments of the nbm-maven-plugin as illustrated below.
Notice the use of -J-Dlog4j.configuration=... the extra -J ensures that the option is passed to the application, otherwise it only affects the JVM of the maven launcher.
log4j.xml
的位置相当奇怪。我将它放在 src/main/resources 中,问题就会消失。如果您需要一些旧版工具来查找所需的相同文件,请尝试在generate-resources
阶段将其复制到那里。例如,使用 maven-antrun-pluginLocation of
log4j.xml
is rather strange. I would place it insrc/main/resources
and the problem would disappear. If you need some legacy tools to find the same file where they need it, try to copy it there ingenerate-resources
phase. For example, with maven-antrun-plugin