Java log4j.properties 被忽略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3444446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j.properties being ignored
提问by kgrad
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.
我在 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 进行日志记录。
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.
以前当我通过 netbeans 或 eclipse 部署时它工作正常,但是当我使用 IntelliJ IDEA 部署时,我的 log4j.properties 文件被忽略,并且 glassfish 的日志记录处理我的日志消息。我不认为 IDEA 与它有任何关系,一定是其他东西发生了变化,我只是不知道是什么。
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?
我已经验证我的 log4j.properties 文件在我的 WEB-INF/classes 目录中,slf4j/log4j jars 在我的War的 WEB-INF/lib 目录中。我是否缺少某种配置才能使其正常工作?
thanks.
谢谢。
edit: Updated with more info, posted pom dependencies.
编辑:更新了更多信息,发布了 pom 依赖项。
Here is the relevant section from my pom.xml:
这是我的 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>
采纳答案by Pascal Thivent
Update:I tried to reproduce the issue. I created a simple Wicket project (same version as you):
更新:我试图重现这个问题。我创建了一个简单的 Wicket 项目(与您的版本相同):
mvn archetype:create \
-DarchetypeGroupId=org.apache.wicket \
-DarchetypeArtifactId=wicket-archetype-quickstart \
-DarchetypeVersion=1.4.9 \
-DgroupId=com.mycompany \
-DartifactId=my-wicketapp
Which has a simple log4j.properties logging to the standard output.
其中有一个简单的 log4j.properties 记录到标准输出。
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n
log4j.rootLogger=INFO,Stdout
log4j.logger.org.apache.wicket=INFO
log4j.logger.org.apache.wicket.protocol.http.HttpSessionStore=INFO
log4j.logger.org.apache.wicket.version=INFO
log4j.logger.org.apache.wicket.RequestCycle=INFO
Then:
然后:
- I added all your dependencies (or modified the versions of existing one to match yours)
- I just did some cleanup e.g. in the Hibernate dependencies, you don't need to declare them all, leverage the transitive dependencies mechanism
- I added relevant
repositories
andpluginRepositories
- I added glassfish's
javax.servlet
dependency to make the build pass - I added the
embedded-glassfish
plugin to test the whole thing - I made a few other unrelated changes
- I changed the compiler settings to 1.6
- I declared
slf4j-api
in thedependencyManagement
element to control nicely the version in transitive dependencies.
- 我添加了您的所有依赖项(或修改了现有依赖项的版本以匹配您的依赖项)
- 我只是做了一些清理,例如在 Hibernate 依赖项中,您不需要全部声明它们,利用传递依赖项机制
- 我添加了相关
repositories
和pluginRepositories
- 我添加了 glassfish 的
javax.servlet
依赖项以使构建通过 - 我添加了
embedded-glassfish
插件来测试整个事情 - 我做了一些其他不相关的更改
- 我将编译器设置更改为 1.6
- 我
slf4j-api
在dependencyManagement
元素中声明以很好地控制传递依赖项中的版本。
The full pom.xml looks like this (so anybody can reproduce):
完整的 pom.xml 看起来像这样(所以任何人都可以复制):
<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.mycompany</groupId>
<artifactId>my-wicketapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<!-- TODO project name -->
<name>quickstart</name>
<description/>
<!--
TODO <organization> <name>company name</name> <url>company url</url>
</organization>
-->
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<repositories>
<!-- For Hibernate Artifacts -->
<repository>
<id>repository.jboss.org-public</id>
<name>JBoss repository</name>
<url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>
<!-- repository for Java EE 6 Binaries -->
<repository>
<id>java.net2</id>
<name>Repository hosting the jee6 artifacts</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
<pluginRepositories>
<!-- GlassFish repository for the embedded-glassfish plugin -->
<pluginRepository>
<id>glassfish</id>
<name>GlassFish Maven 2 Repository</name>
<url>http://download.java.net/maven/glassfish</url>
</pluginRepository>
</pluginRepositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.5-Final</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>r05</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<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 DEPENDENCIES -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket</artifactId>
<version>${wicket.version}</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-auth-roles</artifactId>
<version>${wicket.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-wicket</artifactId>
<version>1.0.1-Final</version>
</dependency>
<!--
OPTIONAL <dependency> <groupId>org.apache.wicket</groupId>
<artifactId>wicket-extensions</artifactId>
<version>${wicket.version}</version> </dependency>
-->
<!-- LOGGING DEPENDENCIES - LOG4J -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- JUNIT DEPENDENCY FOR TESTING -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<!-- GLASSFISH EMBEDDED FOR TESTING -->
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
<!-- JETTY DEPENDENCIES FOR TESTING -->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-management</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<filtering>false</filtering>
<directory>src/main/resources</directory>
</resource>
<resource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<testResources>
<testResource>
<filtering>false</filtering>
<directory>src/test/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<optimize>true</optimize>
<debug>true</debug>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
<plugin>
<groupId>org.glassfish</groupId>
<artifactId>maven-embedded-glassfish-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<serverID>server</serverID>
<name>server</name>
<app>${project.build.directory}/${project.build.finalName}.war</app>
<port>8080</port>
<instanceRoot>${project.build.directory}/gfe-${maven.build.timestamp}</instanceRoot>
<!--contextRoot>${build.finalName}</contextRoot-->
<autoDelete>true</autoDelete>
<!--configFile>${basedir}/domain.xml</configFile-->
</configuration>
</plugin>
</plugins>
</build>
<properties>
<wicket.version>1.4.9</wicket.version>
<jetty.version>6.1.4</jetty.version>
<slf4j.version>1.5.6</slf4j.version>
</properties>
</project>
And when I run the project with the embedded-glassfish plugin:
当我使用嵌入式 glassfish 插件运行项目时:
$ mvn package ... $ mvn embedded-glassfish:run ...
and access http://localhost:8080/serverin a browser, I get my logs in the standard output as expected:
并在浏览器中访问http://localhost:8080/server,我按预期在标准输出中获取日志:
... INFO: [WicketApplication] Started Wicket version 1.4.9 in development mode ******************************************************************** *** WARNING: Wicket is running in DEVELOPMENT mode. *** *** ^^^^^^^^^^^ *** *** Do NOT deploy to your live server(s) without changing this. *** *** See Application#getConfigurationType() for more information. *** ********************************************************************
I wonder if this is representative or not.
我想知道这是否具有代表性。
I have checked the war, log4j.properties is indeed in WEB-INF/classes. I don't have a log4j.jar, i have slf4j-log4j12.jar.
我查过war,log4j.properties确实在WEB-INF/classes中。我没有log4j.jar,我有slf4j-log4j12.jar。
slf4j-log4j12.jar is not a replacement for log4j.jar, slf4j-log4j12.jar is a bindingfor log4J version 1.2, you still need log4j.jar. From the SLF4J documentation:
slf4j-log4j12.jar 不是 log4j.jar 的替代品,slf4j-log4j12.jar 是log4J 1.2 版本的绑定,你仍然需要 log4j.jar。从 SLF4J 文档:
Binding with a logging framework at deployment time
As mentioned previously, SLF4J supports various logging frameworks. The SLF4J distribution ships with several jar files referred to as "SLF4J bindings", with each binding corresponding to a supported framework.
slf4j-log4j12-1.6.1.jar: Binding for log4j version 1.2, a widely used logging framework. You also need to place log4j.jar on your class path.
在部署时绑定日志框架
如前所述,SLF4J 支持各种日志框架。SLF4J 发行版附带了几个称为“SLF4J 绑定”的 jar 文件,每个绑定对应一个受支持的框架。
slf4j-log4j12-1.6.1.jar:绑定 log4j 1.2 版,这是一个广泛使用的日志框架。您还需要将 log4j.jar 放在您的类路径上。
I wonder how you got this working under NetBeans and Eclipse.
我想知道你是如何在 NetBeans 和 Eclipse 下运行的。
回答by Paul Jowett
The two most likely things that spring to mind are:
最有可能想到的两件事是:
- make sure the log4j.properties file is in WEB-INF/classes of the deployment. I assume you mean it is in your WEB-INF/classes in your codebase, but have you confirmed this is the case in the war that sent to glassfish
- make sure your log4j.jar is in your deployed WEB-INF/lib
- 确保 log4j.properties 文件位于部署的 WEB-INF/classes 中。我假设你的意思是它在你的代码库中的 WEB-INF/类中,但你是否确认这是发送给 glassfish 的War中的情况
- 确保您的 log4j.jar 在您部署的 WEB-INF/lib 中
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.
由于它在某些部署场景中工作,我怀疑您在通过 Maven 运行时的War打包是问题所在。以上几点应该可以帮助您确认这一点。
回答by FrVaBe
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.
看看log4j 手册。“默认初始化过程”一节描述了 log4j 将如何尝试查找初始化文件。也许你可以尝试一些这样的选项来让事情工作。
回答by Adriaan Koster
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.
我建议删除所有 slf4j 依赖项并更改您的日志记录代码以直接使用 Log4j API(如果这不是太多工作,不知道您的项目大小)。一旦成功,请考虑您是否真的需要 slf4j 提供的灵活性。如果这样做,请选择正确版本的 slf4j(slf4j-log4j12 可能不是用于 log4j 1.2.16 的正确版本)并将其重新集成。
I encountered slf4j recently, and in the end removed it altogether because I ran into similar configuration problems.
最近遇到slf4j,最后因为碰到类似的配置问题,干脆把它删了。
回答by bert
I have the following logging dependencies :
我有以下日志记录依赖项:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>0.9.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>runtime</scope>
</dependency>
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
在 /src/main/resources/ 下,我有一个 logback.xml 定义了各个方面(附加程序,..)。这被 maven 接收并复制到 WEB-INF/classes
hope that helped
希望有所帮助
回答by Denis Kniazhev
I would support Adriaan Koster's answer. If your pure log4j doesn't work however, try the following. Create simple class like this
我会支持 Adriaan Koster 的回答。但是,如果您的纯 log4j 不起作用,请尝试以下操作。像这样创建简单的类
import org.apache.log4j.Logger;
public class LogTest {
private static Logger log;
public static void main(String[] args) {
log = Logger.getLogger(LogTest.class);
}
}
...and put breakpoint in org.apache.log4j.helpers.Loader#getResource method. It tries to pull log4j.xml from classloader:
...并在 org.apache.log4j.helpers.Loader#getResource 方法中放置断点。它尝试从类加载器中提取 log4j.xml:
url = classLoader.getResource(resource);
So you can easily look inside the classloader and see what paths it uses (and why it can't find your log4j.xml).
因此,您可以轻松查看类加载器内部并查看它使用的路径(以及为什么找不到您的 log4j.xml)。
回答by dmatej
I had the same problems. The solution is simple - all logging dependencies should be before the glassfish on the classpath.
我有同样的问题。解决方案很简单——所有日志依赖项都应该在类路径上的 glassfish 之前。
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).
请注意,较旧的 Maven2 版本在类路径一致性方面存在一些问题。我使用 2.2.1 已修复此问题(我认为它已在 2.0.9 中修复)。
回答by electrotype
I had the exact same problem.
我有同样的问题。
Log4j alone works great:
单独使用 Log4j 效果很好:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
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 ison the classpath.
但是,如果我尝试使用SLF4J过它,那么我的“SRC /资源/ log4j.properties”文件找不到了,即使Maven的将其添加到目录是在classpath。
So, this doesn't work out of the box:
所以,这不是开箱即用的:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
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):
要使其工作,您必须将“log4j.properties”显式添加到类路径或告诉服务器在哪里可以找到它!实现此目的的一种方法是(此示例在 Windows 上):
-Dlog4j.configuration=file:C:\[pathToYourProject]\trunk\target\classes\log4j.properties
In Eclipse (if this is what you use), you can add the same line to your Run Configuration / VM arguments.
在 Eclipse 中(如果这是您使用的),您可以将同一行添加到您的运行配置/VM 参数中。
回答by Micha? Rowicki
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.
在项目中使用的库的所有模块之后编译 log4j 库也非常重要。如果您不将其设置为最后一个对象,则后续模块/库中的日志将不会以 log4j.properties 的标准方式显示。