使用 Tomcat 6 在 Spring Web 应用程序中设置 Commons Logging / Log4j 的问题

发布于 2024-07-06 18:56:42 字数 1481 浏览 5 评论 0原文

我在 tomcat 6 下部署的 apring web 应用程序中的日志记录设置有问题。

该 web 应用程序使用 commons-logging api,在运行时应使用 log4j。 日志文件已创建但仍为空 - 没有日志条目发生。

设置如下:

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:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

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>

文件logs/my.log已创建,但没有日志出现。 这些是 tomcat 控制台上的信息日志,但未配置布局模式。

commons-logging-1.1.1.jar 和 log4j-1.2.14.jar 包含在 WEB-INF/lib 中。 知道这里出了什么问题吗?

I have a problem wih a logging setup in a apring webapp deployed under tomcat 6.

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.

the setup is the following:

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:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

从﹋此江山别 2024-07-13 18:56:42

网络上有许多记录在案的实例,警告人们不要使用公共日志记录。 正因如此,SLF4J 越来越受欢迎。

考虑到您对将 Tomcat 与 Log4j 一起使用不感兴趣,您应该直接在应用程序中使用 Log4j。 特别是如果您将来没有机会切换日志框架的话。 它将降低应用程序的复杂性,并消除您在公共日志记录方面遇到的任何类加载器问题。

这应该是文本中相对容易的搜索和替换,因为 commons-logging 和 log4j 都对其日志记录方法使用类似的调用结构。

There are numerous documented instances on the web warning people about the use of commons-logging. So much so, that SLF4J is gaining a lot of popularity.

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.

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.

童话里做英雄 2024-07-13 18:56:42

请特别注意,您没有将 log4j.jar 放置在 Tomcat commons/lib 目录中。 如果根类加载器加载 log4j 库,那么当您的 Web 应用程序也尝试使用 log4j 时,您将遇到冲突和初始化问题。

如果您需要使用 log4j 进行常见的 Tomcat 日志记录,则需要注意您的 Web 应用程序不要尝试加载 log4j。 如果服务器上有多个 Web 应用程序,那么您需要遵守规则,确保每个 Web 应用程序的日志初始化不会影响其他 Web 应用程序的初始化。 每个 Web 应用程序都需要使用唯一的记录器 ID,这可以通过唯一的包名称来完成。

当您拥有所有都想要进行日志记录的共享库(例如 Hibernate 或 Spring)时,在 Tomcat 中与多个 Web 应用程序一起使用通用 log4j 会导致严重冲突。 下一个尝试初始化 log4j 的 Web 应用程序可能会关闭前一个 Web 应用程序的记录器。 这可能会很混乱。

Be especially careful that you have not placed 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.

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.

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.

终陌 2024-07-13 18:56:42

我有类似的问题,现在找到了解决办法。
使用附加参数启动 tomcat:

-Dorg.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl

I had similar problem and found a fix now.
Start tomcat with additional parameter:

-Dorg.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl

季末如歌 2024-07-13 18:56:42

您需要编译额外的组件以实现完整的公共日志记录。 默认情况下,Tomcat 6 使用 commons-logging 的硬编码实现,该实现始终委托给 java.util.logging。

此处的构建说明 http://tomcat.apache.org/tomcat-6.0-doc /building.html

然后替换Tomcat的/bin目录下的tomcat-juli.jar,并将tomcat-juli-adapters.jar与log4j和config一起放置在/lib目录下。

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.

Building instructions here 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.

耳钉梦 2024-07-13 18:56:42

如果您使用 log4j +common 日志记录,则可以避免上述大部分配置。 常见的日志记录 LogFactory 具有类似于 JAXP 的发现功能,按照以下优先顺序,搜索 Log 实现,
1.配置属性org.apache.commons.logging.Log在文件commons-logging.properties中
2.系统属性org.apache.commons.logging.Log
3. 如果 Log4J 在类路径中可用,则使用相应的包装类 (Log4JLogger)。
4.Jdk14Logger
5. SimpleLog

只需确保 common-logging.jar 和 common-logging-api.jar 以及 log4j.jar 位于类路径中。

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

just make sure, both common-logging.jar and common-logging-api.jar and log4j.jar at classpath.

够钟 2024-07-13 18:56:42

要对公共日志进行故障排除,系统属性 org.apache.commons.logging.diagnostics.dest 允许将输出诊断信息转储到目标文件(我不知道来自哪个版本)。

To troubleshoot commons logging the system property org.apache.commons.logging.diagnostics.dest allows to dump output diagnostic information to a destination file (I'm not aware from which version).

濫情▎り 2024-07-13 18:56:42

也许我错了。 请尝试以下操作:

A)将附加程序添加到 my.package 中,如下所示:

或者

B) 将root的日志级别降低为INFO

May be i am wrong. Please try the following:

A) Add appender to my.package as:

OR

B) Reduce the log leve of root to INFO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文