Java 使用tomcat 6在spring webapp中设置Commons Logging / Log4j的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/217929/
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
Problem with Commons Logging / Log4j setup in spring webapp with tomcat 6
提问by Arne Burmeister
I have a problem wih a logging setup in a apring webapp deployed under tomcat 6.
我在 tomcat 6 下部署的 apring webapp 中的日志设置有问题。
The webapp uses the commons-logging api, on runtime log4j should be used. The log file is created but remains empty - no log entries occur.
webapp 使用 commons-logging api,在运行时应该使用 log4j。日志文件已创建但仍为空 - 不会出现日志条目。
the setup is the following:
设置如下:
WEB-INF/web.xml:
WEB-INF/web.xml:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
WEB-INF/classes/commons-logging.properties:
WEB-INF/classes/commons-logging.properties:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
WEB-INF/log4j.xml:
WEB-INF/log4j.xml:
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
...
</appender>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${catalina.home}/logs/my.log"/>
...
</appender>
<logger name="my.package">
<level value="INFO"/>
</logger>
<root>
<level value="ERROR"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</log4j:configuration>
The file logs/my.log is created, but no logs appear. The are info logs on the tomcat console, but not with the layout pattern configured.
文件 logs/my.log 已创建,但未显示任何日志。在 tomcat 控制台上有信息日志,但没有配置布局模式。
The commons-logging-1.1.1.jar and log4j-1.2.14.jar are included in WEB-INF/lib. Any idea what is wrong here?
commons-logging-1.1.1.jar 和 log4j-1.2.14.jar 包含在 WEB-INF/lib 中。知道这里有什么问题吗?
采纳答案by Spencer Kormos
There are numerous documented instances on the web warning people about the use of commons-logging. So much so, that SLF4Jis gaining a lot of popularity.
网络上有许多记录在案的实例,警告人们使用 commons-logging。如此之多,以至于SLF4J越来越受欢迎。
Considering that you are not interested in using Tomcat with Log4j, you should just use Log4j directly in your application. Particularly if there is no chance that you'll be switching logging frameworks in the future. It'll reduce the complexity of your application, and get rid of any class loader issues you are having with commons-logging.
考虑到您对在 Log4j 中使用 Tomcat 不感兴趣,您应该直接在您的应用程序中使用 Log4j。特别是如果您将来不可能切换日志记录框架。它将降低您的应用程序的复杂性,并摆脱您在 commons-logging 中遇到的任何类加载器问题。
This should be a relatively easy search and replace in your text, as commons-logging and log4j both use a similar call structure for their logging methods.
这应该是一个相对容易的搜索和替换文本,因为 commons-logging 和 log4j 都使用类似的调用结构作为它们的日志记录方法。
回答by Jonas K
You need to compile the extra component for full commons-logging. By default Tomcat 6 uses a hardcoded implementation of commons-logging that always delegates to java.util.logging.
您需要编译额外的组件以进行完整的公共日志记录。默认情况下,Tomcat 6 使用 commons-logging 的硬编码实现,它始终委托给 java.util.logging。
Building instructions here http://tomcat.apache.org/tomcat-6.0-doc/building.html
此处构建说明http://tomcat.apache.org/tomcat-6.0-doc/building.html
Then replace the tomcat-juli.jar in the /bin directory of Tomcat and place the tomcat-juli-adapters.jar in the /lib directory along with log4j and config.
然后替换Tomcat/bin目录下的tomcat-juli.jar,将tomcat-juli-adapters.jar和log4j、config放在/lib目录下。
回答by Mojo
Be especially careful that you have notplaced log4j.jar in the Tomcat commons/lib directory. If the root classloader loads the log4j libraries, you'll run into conflicts and initialization problems when your webapps also try to use log4j.
请特别注意您没有将 log4j.jar 放在 Tomcat commons/lib 目录中。如果根类加载器加载 log4j 库,当您的 web 应用程序也尝试使用 log4j 时,您将遇到冲突和初始化问题。
If you need to use log4j for common Tomcat logging, you'll need to be careful that your webapps do not attempt to load log4j as well. If you have multiple webapps on the server, then you'll need discipline that each webapp's log initialization does not stomp on the initialization of other webapps. Each webapp will need to use unique Logger IDs, which can be accomplished with unique package names.
如果您需要将 log4j 用于常见的 Tomcat 日志记录,则需要注意您的 web 应用程序也不要尝试加载 log4j。如果您在服务器上有多个 webapp,那么您需要遵守纪律,即每个 webapp 的日志初始化不会影响其他 webapp 的初始化。每个 web 应用程序都需要使用唯一的 Logger ID,这可以通过唯一的包名称来实现。
Using a common log4j in Tomcat with multiple webapps causes serious conflicts when you have shared libraries that all want to do logging, such as Hibernate or Spring. The next webapp that attempts to initialize log4j may close the logger of the previous one. It can be a mess.
当您拥有所有都想进行日志记录的共享库(例如 Hibernate 或 Spring)时,在具有多个 web 应用程序的 Tomcat 中使用公共 log4j 会导致严重的冲突。下一个尝试初始化 log4j 的 webapp 可能会关闭前一个的记录器。这可能是一团糟。
回答by AGEORGE
May be i am wrong. Please try the following:
可能是我错了。请尝试以下操作:
A) Add appender to my.package as: ORB) Reduce the log leve of root to INFO
A) 将 appender 添加到 my.package 为:或B) 将 root 的日志级别 降低到 INFO
回答by lwpro2
if you are using log4j +common logging, you can avoid most of above configurations. common logging LogFactory have a discovery feature similar to JAXP, in following precedence, searching for Log implementations, 1. configuration attribute org.apache.commons.logging.Log inside file commons-logging.properties 2. system property org.apache.commons.logging.Log 3. If the Log4J available at class path, use the corresponding wrapper class (Log4JLogger). 4. Jdk14Logger 5. SimpleLog
如果您使用 log4j +common logging,则可以避免上述大部分配置。common logging LogFactory 有一个类似于 JAXP 的发现功能,在以下优先级中,搜索 Log 实现, 1. 文件 commons-logging.properties 中的配置属性 org.apache.commons.logging.Log 2. 系统属性 org.apache.commons。 logging.Log 3. 如果Log4J 在类路径可用,则使用相应的包装类(Log4JLogger)。4. Jdk14Logger 5. SimpleLog
just make sure, both common-logging.jar and common-logging-api.jar and log4j.jar at classpath.
只需确保类路径中的 common-logging.jar 和 common-logging-api.jar 和 log4j.jar 。
回答by Senthil
I had similar problem and found a fix now. Start tomcat with additional parameter:
我遇到了类似的问题,现在找到了解决方法。使用附加参数启动 tomcat:
-Dorg.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
-Dorg.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl