Spring 类忽略工作 log4j 配置

发布于 2024-08-18 23:35:46 字数 2085 浏览 5 评论 0原文

我使用 log4j,他工作得很好。 log4j.xml 文件位于我的类路径中,我使用附加程序和类别...再次,它对于我的代码或其他库的代码工作得很好。

但是春季课程继续用我想在其他地方记录的消息淹没标准输出。它开始变得烦人了。

违规消息:org.springframework.jms:一些错误...... 更一般地说,org.springframework 包的所有类都会向 stdout 发送消息。

所以我把它放在我的 log4j.xml 中:

<appender name="JMS-ERROR" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="tms.logs.folder/tms.logs.files.prefix-jms-error.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="1000KB"/>
    <param name="MaxBackupIndex" value="2"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="**DVIDEA** %d{dd.MM.yyyy HH:mm:ss} %c  %m%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="FATAL" />
    </filter>
</appender>


<category name="org.springframework.jms" additivity="false">
    <appender-ref ref="JMS-ERROR"/>
</category>

它可以与其他库一起使用。为什么不是春天呢?

我做了一些研究,看来 Spring 使用通用日志记录,而不是 log4j。这是一个可能的原因吗?有什么解决方法吗?

我尝试将 log4jConfigurationListener 添加到我的 web.xml 中。他正在工作,但我仍然无法控制弹簧错误消息。

<context-param>
   <param-name>log4jConfigLocation</param-name>
   <param-value>classpath:log4j.xml</param-value>
</context-param>
<context-param>
   <param-name>log4jRefreshInterval</param-name>
   <param-value>10000</param-value>
</context-param>
<listener>
   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

编辑:

更多信息:

  • 我使用 Tomcat 5.5,
  • 由于历史原因,log4j.property(旧的 log4j 配置)仍然存在。我尝试删除它=>没有变化。

编辑2: 我使用以下解决方法,但我对此不满意:它不可配置

java.util.logging.Logger springLogger = Logger.getLogger("org.springframework.jms");
springLogger.setLevel(Level.OFF);

I use log4j and he work just fine. A log4j.xml file is in my classpath, i use appender and category... again, it's work just fine for my code or code of other librairy.

But the spring classes continue to flood stdout with message i want to log elsewhere. It's beggin to be anoying.

Offending message : org.springframework.jms : some error....
More generally, all classes of the org.springframework package send message to stdout.

So i put this in my log4j.xml :

<appender name="JMS-ERROR" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="tms.logs.folder/tms.logs.files.prefix-jms-error.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="1000KB"/>
    <param name="MaxBackupIndex" value="2"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="**DVIDEA** %d{dd.MM.yyyy HH:mm:ss} %c  %m%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="FATAL" />
    </filter>
</appender>


<category name="org.springframework.jms" additivity="false">
    <appender-ref ref="JMS-ERROR"/>
</category>

It's work with other librairie. Why not spring ?

I do some research, and it's appeart that Spring use common-logging, and not log4j. It's that a possible cause ? Any workaround ?

I have try to add a log4jConfigurationListener to my web.xml. he's working but i stil dont have any control over the spring error message.

<context-param>
   <param-name>log4jConfigLocation</param-name>
   <param-value>classpath:log4j.xml</param-value>
</context-param>
<context-param>
   <param-name>log4jRefreshInterval</param-name>
   <param-value>10000</param-value>
</context-param>
<listener>
   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

Edit :

Some more info :

  • i use Tomcat 5.5
  • a log4j.property ( old log4j config ) is still present for historical reason. I'v try to remove it => no change.

EDIT 2 :
I use the following workaround, but i'm not happy with it : it's not configurable

java.util.logging.Logger springLogger = Logger.getLogger("org.springframework.jms");
springLogger.setLevel(Level.OFF);

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

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

发布评论

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

评论(4

一绘本一梦想 2024-08-25 23:35:46

请注意,在 Spring 示例中,他们使用 slf4j 将日志消息从 Commons Logging 路由到 log4j。在 Maven 的 pom.xml 中,它看起来像这样:

<!-- Exclude Commons Logging -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>org.springframework.context</artifactId>
    <version>${spring.version}</version>
    <exclusions>
        <!-- Exclude Commons Logging in favor of SLF4j -->
        <exclusion>
            <groupId>org.apache.commons</groupId>
            <artifactId>com.springsource.org.apache.commons.logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- Add slf4j API -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>com.springsource.slf4j.api</artifactId>
    <version>${slf4j.version}</version>
</dependency>

<!-- Add Commons Logging -> slf4j bridge -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>com.springsource.slf4j.org.apache.commons.logging</artifactId>
    <version>${slf4j.version}</version>
    <scope>runtime</scope>
</dependency>

<!-- Add slf4j -> log4j bridge -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>com.springsource.slf4j.log4j</artifactId>
    <version>${slf4j.version}</version>
    <scope>runtime</scope>
</dependency>

Note that in the Spring samples they are using slf4j to route log messages from Commons Logging to log4j. In Maven's pom.xml it looks this way:

<!-- Exclude Commons Logging -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>org.springframework.context</artifactId>
    <version>${spring.version}</version>
    <exclusions>
        <!-- Exclude Commons Logging in favor of SLF4j -->
        <exclusion>
            <groupId>org.apache.commons</groupId>
            <artifactId>com.springsource.org.apache.commons.logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- Add slf4j API -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>com.springsource.slf4j.api</artifactId>
    <version>${slf4j.version}</version>
</dependency>

<!-- Add Commons Logging -> slf4j bridge -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>com.springsource.slf4j.org.apache.commons.logging</artifactId>
    <version>${slf4j.version}</version>
    <scope>runtime</scope>
</dependency>

<!-- Add slf4j -> log4j bridge -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>com.springsource.slf4j.log4j</artifactId>
    <version>${slf4j.version}</version>
    <scope>runtime</scope>
</dependency>
灯角 2024-08-25 23:35:46

您可能需要:

<bean id="log4jInitialization"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass"
            value="org.springframework.util.Log4jConfigurer" />
        <property name="targetMethod" value="initLogging" />
        <property name="arguments">
            <list>
                <value>conf/log4j.xml</value>
            </list>
        </property>
    </bean>

You might need:

<bean id="log4jInitialization"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass"
            value="org.springframework.util.Log4jConfigurer" />
        <property name="targetMethod" value="initLogging" />
        <property name="arguments">
            <list>
                <value>conf/log4j.xml</value>
            </list>
        </property>
    </bean>
怪我入戏太深 2024-08-25 23:35:46

Spring 使用 Apache Commons Logging,后者又决定是使用 STDOUT(通过 java.util.logging)还是 Log4j。如果您在 STDOUT 上获得 Spring 输出,则由于某种原因,commons-logging 尚未找到 log4j。

如果您设置系统属性 org.apache.commons.logging.diagnostics.dest,则可以将 commons-logging 配置为记录其诊断信息,告诉您它正在采取哪些步骤来确定 log4j 中是否存在。

所以在你的tomcat启动脚本中,设置系统属性,例如

-Dorg.apache.commons.logging.diagnostics.dest=STDOUT

Spring uses Apache Commons Logging, which in turn decides whether to use STDOUT (via java.util.logging) or Log4j. If you're getting Spring output on STDOUT, then commons-logging hasn't located log4j for some reason.

If you set the system property org.apache.commons.logging.diagnostics.dest, commons-logging can be configured to log its diagnostics, telling you what steps it is taking to determine in log4j is present.

So in your tomcat startup script, set the system property, e.g.

-Dorg.apache.commons.logging.diagnostics.dest=STDOUT
幼儿园老大 2024-08-25 23:35:46

您必须使用 SLF4J,并包含通往您想要的日志记录平台 (log4j) 的桥接器,但您还必须从可能引入它的所有其他位置排除 commons-logging。拥有一个 IDE 会有所帮助为此,或者您可以放入一个虚拟条目以进行公共日志记录。有关详细信息,请参阅 SLF4J FAQ 条目

之前使用 com.springsource 工件提到的答案,但是,如果您使用 Maven Central,正确的方法是:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.10</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.10</version>
</dependency>

You must use SLF4J, and include the bridge to the logging platform you want (log4j) but you must also exclude commons-logging from all the other places it might be pulled in. It helps to have an IDE to do this, or you can put a dummy entry in for commons-logging. For details on that, see the SLF4J FAQ entry

A previous answer mentioned using the com.springsource artifacts, however if you are using Maven Central the right way to do it is:

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