log4j.properties 被忽略
我在 glassfish v3.0.1 上使用 IntelliJ IDEA 9.0.3 使用 Maven 部署了 Java EE 6 Wicket 应用程序。我使用 slf4j-log4j12-1.5.6 与 slf4j-api-1.5.8 和 log4j-1.2.16 进行日志记录。
以前,当我通过 netbeans 或 eclipse 部署时,它工作正常,但是当我使用 IntelliJ IDEA 部署时,我的 log4j.properties 文件被忽略,而 glassfish 的日志记录会处理我的日志消息。我不认为 IDEA 与此有任何关系,一定有其他东西发生了变化,我只是不知道是什么。
我已经验证我的 log4j.properties 文件位于我的 WEB-INF/classes 目录中,并且 slf4j/log4j jar 位于我的 war 的 WEB-INF/lib 目录中。我是否缺少某种配置来使其工作?
谢谢。
编辑:更新了更多信息,发布了 pom 依赖项。
这是我的 pom.xml 中的相关部分:
<!-- Guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>r05</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<!-- Java EE 6 -->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>bean-validator</artifactId>
<version>3.0-JBoss-4.0.0.Beta3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<!-- Wicket -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket</artifactId>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-auth-roles</artifactId>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-wicket</artifactId>
<version>1.0.1-Final</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.5.1-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.1-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.1-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.5.1-Final</version>
</dependency>
<!-- Database -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-701.jdbc4</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
I have a Java EE 6 Wicket application deployed with maven using IntelliJ IDEA 9.0.3 on glassfish v3.0.1. I use slf4j-log4j12-1.5.6 with slf4j-api-1.5.8 and log4j-1.2.16 for logging.
It was previously working fine when I deployed through netbeans or eclipse, however when I deploy with IntelliJ IDEA my log4j.properties file is ignored and glassfish's logging handles my log messages. I do not think IDEA has anything to do with it, something else must have changed I just can't figure out what.
I have verified that my log4j.properties file is in my WEB-INF/classes directory and the slf4j/log4j jars are in the WEB-INF/lib directory of my war. Is there some sort of configuration I am missing to make this work?
thanks.
edit: Updated with more info, posted pom dependencies.
Here is the relevant section from my pom.xml:
<!-- Guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>r05</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<!-- Java EE 6 -->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>bean-validator</artifactId>
<version>3.0-JBoss-4.0.0.Beta3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<!-- Wicket -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket</artifactId>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-auth-roles</artifactId>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-wicket</artifactId>
<version>1.0.1-Final</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.5.1-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.1-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.1-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.5.1-Final</version>
</dependency>
<!-- Database -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-701.jdbc4</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
更新:我尝试重现该问题。我创建了一个简单的 Wicket 项目(与您版本相同):
其中有一个简单的 log4j.properties 记录到标准输出。
然后:
repositories
和pluginRepositories
javax.servlet
依赖项来进行构建过程embedded-glassfish
插件来测试整个事情dependencyManagement
元素中声明了slf4j-api
,以很好地控制传递依赖项中的版本。完整的 pom.xml 看起来像这样(所以任何人都可以重现):
当我使用嵌入的 glassfish 插件运行项目时:
并访问 http://localhost:8080/server 在浏览器中,我按预期在标准输出中获取日志:
我想知道这是否具有代表性。
slf4j-log4j12.jar 不是 log4j.jar 的替代品,slf4j-log4j12.jar 是 log4J 版本 1.2 的绑定,您仍然需要 log4j.jar。来自 SLF4J 文档:
我想知道您是如何在 NetBeans 和 Eclipse 下实现此功能的。
Update: I tried to reproduce the issue. I created a simple Wicket project (same version as you):
Which has a simple log4j.properties logging to the standard output.
Then:
repositories
andpluginRepositories
javax.servlet
dependency to make the build passembedded-glassfish
plugin to test the whole thingslf4j-api
in thedependencyManagement
element to control nicely the version in transitive dependencies.The full pom.xml looks like this (so anybody can reproduce):
And when I run the project with the embedded-glassfish plugin:
and access http://localhost:8080/server in a browser, I get my logs in the standard output as expected:
I wonder if this is representative or not.
slf4j-log4j12.jar is not a replacement for log4j.jar, slf4j-log4j12.jar is a binding for log4J version 1.2, you still need log4j.jar. From the SLF4J documentation:
I wonder how you got this working under NetBeans and Eclipse.
我遇到了完全相同的问题。
Log4j 单独工作就很好:
但是如果我尝试在它上面使用 Slf4j,那么我的“src/resources/log4j.properties”文件就找不到了,即使 Maven 将其添加到 的目录中类路径。
因此,这并不是开箱即用的:
要使其工作,您必须显式地将“log4j.properties”添加到类路径中或告诉服务器在哪里可以找到它!实现此目的的一种方法是(此示例在 Windows 上):
在 Eclipse 中(如果您使用的是这样的),您可以将同一行添加到运行配置/VM 参数中。
I had the exact same problem.
Log4j alone works great:
But if I try to use Slf4j over it, then my "src/resources/log4j.properties" file is not found anymore, even if Maven add it to a directory that is on the classpath.
So, this doesn't work out of the box:
To make it work, you have to explicitly add the "log4j.properties" to the classpath or tell the server where to find it! A way to achieve this is (this example is on Windows):
In Eclipse (if this is what you use), you can add the same line to your Run Configuration / VM arguments.
我建议删除所有 slf4j 依赖项并更改您的日志记录代码以直接使用 Log4j API(如果这不是太多工作,不知道您的项目的大小)。一旦它起作用,请考虑您是否真的需要 slf4j 提供的灵活性。如果这样做,请选择正确版本的 slf4j(slf4j-log4j12 可能不是用于 log4j 1.2.16 的正确版本)并将其重新集成。
我最近遇到了 slf4j,最后完全删除了它,因为我运行了陷入类似的配置问题。
I suggest removing all the slf4j dependencies and change your logging code to use the Log4j API directly (if this is not too much work, don't know the size of your project). Once this works, consider if you really need the flexibility offered by slf4j. If you do, pick up the correct version of slf4j (slf4j-log4j12 might not be the correct one to use for log4j 1.2.16) and integrate it back in.
I encountered slf4j recently, and in the end removed it altogether because I ran into similar configuration problems.
我也有同样的问题。解决方案很简单 - 所有日志依赖项都应该位于类路径上的 glassfish 之前。
请注意,较旧的 Maven2 版本在类路径一致性方面存在一些问题。我使用的 2.2.1 已修复此问题(我认为它已在 2.0.9 中修复)。
I had the same problems. The solution is simple - all logging dependencies should be before the glassfish on the classpath.
Note that older Maven2 versions have some problems with the classpath consistency. I use 2.2.1 which have this issue fixed (it was fixed in 2.0.9, I think).
查看 log4j 手册。 “默认初始化过程”部分描述了 log4j 如何尝试查找初始化文件。也许您可以尝试其中一些选项来使事情正常进行。
Have a look at the log4j manual. The section "Default Initialization Procedure" describes how log4j will try to find the initialization file. Maybe you can try some of this options to get things work.
最可能想到的两件事是:
因为它正在工作在某些部署场景中,我怀疑通过 Maven 运行时的 war 打包是问题所在。上述几点应该可以帮助您确认这一点。
The two most likely things that spring to mind are:
Since it's working in some deployment scenarios, I suspect your war packaging when running via Maven is the problem. The above points should help you confirm this.
我有以下日志依赖项:
在 /src/main/resources/ 下,我有一个 logback.xml 定义各个方面(附加程序,..)。这被 maven 拾取并复制到 WEB-INF/classes
希望有帮助
I have the following logging dependencies :
under /src/main/resources/ i have a logback.xml defining the various aspects (appenders,..). This gets picked up by maven and copied to WEB-INF/classes
hope that helped
我支持阿德里安·科斯特的回答。但是,如果您的纯 log4j 不起作用,请尝试以下操作。创建像这样的简单类
...并在 org.apache.log4j.helpers.Loader#getResource 方法中放置断点。它尝试从类加载器中提取 log4j.xml:
因此您可以轻松地查看类加载器内部并查看它使用的路径(以及为什么它找不到您的 log4j.xml)。
I would support Adriaan Koster's answer. If your pure log4j doesn't work however, try the following. Create simple class like this
...and put breakpoint in org.apache.log4j.helpers.Loader#getResource method. It tries to pull log4j.xml from classloader:
So you can easily look inside the classloader and see what paths it uses (and why it can't find your log4j.xml).
同样非常重要的是,log4j 库是在项目中使用的库的所有模块之后编译的。如果您不将其设置为最后一个对象,则后续模块/库的日志将不会以 log4j.properties 的标准方式显示。
It is also very important that log4j library was compiled after all modules of the libraries used in the project. If you will not set it as last object, the logs from later modules/libraries wouldn't be displayed in a standard way for log4j.properties.
对于使用 spring boot 的人:
它似乎忽略
-Dlog4j.configuration=file:..
。您可以改用-Dlogging.config=file:/etc/myconfig/log4j.xml
。必须是xml格式。您还可以在 java 源目录中编辑文件
logback-spring.xml
,例如src/main/resources/
。这是默认日志记录配置的标准位置。更多信息的良好来源:https://www.baeldung.com/spring-boot-logging
For people using spring boot:
It appears to ignore
-Dlog4j.configuration=file:..
. You can instead use-Dlogging.config=file:/etc/myconfig/log4j.xml
. It must be xml format.You can also edit the file
logback-spring.xml
in your java source dir egsrc/main/resources/
. This is the standard place for the default logging config.A good source of further information: https://www.baeldung.com/spring-boot-logging
对我有用的方法:将 log4j2.properties (或 log4j2.xml)添加到我的类路径根目录(
tomcat/webapps/ROOT/WEB-INF/classes/
)。参数值记录现已启用。背景:我注意到我的构建中有 log4j 1、2 和 slf4j jar。我们在许多 java 文件中都有 slf4j 导入。正在使用 hibernate.properties 并且 hibernate.show_sql=true 可以工作,但我无法启用参数值日志记录。当 mvn 打包的 war 在 tomcat 中运行时,类路径根目录中的 log4j.properties 被忽略。但是当我从 eclipse 运行它时,log4j.properties 工作了,我可以启用参数值日志记录。
使用的 log4j2.properties:
基于:https: //www.codejava.net/frameworks/hibernate/how-to-configure-hibernate-logging-with-log4j2
搜索 log4j/slf4j:
find /usr/local/tomcat/webapps/ROOT -名称 '*slf4j*' -o -name '*log4j*'
What worked for me: Adding log4j2.properties (or log4j2.xml) to my class path root (
tomcat/webapps/ROOT/WEB-INF/classes/
). Parameter-value logging is now enabled.Background: I noticed log4j 1, 2 and slf4j jars in my build. We have slf4j imports in many java files. hibernate.properties was being used and
hibernate.show_sql=true
worked but I could not enable parameter-value logging. My log4j.properties in the classpath root was being ignored - when the mvn packaged war was run in tomcat. But when I ran it from eclipse, log4j.properties worked and I could enable parameter-value logging.log4j2.properties used:
Based on: https://www.codejava.net/frameworks/hibernate/how-to-configure-hibernate-logging-with-log4j2
Search for log4j/slf4j:
find /usr/local/tomcat/webapps/ROOT -name '*slf4j*' -o -name '*log4j*'