如何在第三方jar中禁用log4j?

axi*_*sty 15 java log4j

我正在尝试使用JBoss焊接编写一个java SE swing应用程序.Weld使用jar中的以下log4j.xml文件使用log4j配置日志记录:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!--

    JBoss, Home of Professional Open Source
    Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
    contributors by the @authors tag. See the copyright.txt in the
    distribution for a full listing of individual contributors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <!-- The default pattern: Date Priority [Category] Message\n -->
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{2}] %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.StringMatchFilter">
         <param name="AcceptOnMatch" value="false" />
         <param name="StringToMatch" value="Failure while notifying an observer of event [a]" />
        </filter>
    </appender>

    <!-- ############### Weld logging ################### -->

    <category name="org.jboss.weld">
        <priority value="INFO"/>
    </category>

    <root>
        <priority value="INFO"/>
        <appender-ref ref="CONSOLE"/>
    </root>

</log4j:configuration>
Run Code Online (Sandbox Code Playgroud)

我想在我的应用程序中完全禁用日志记录.我尝试禁用它,提供log4j.properties文件,如下所示:

log4j.debug=FALSE
log4j.rootLogger=OFF, CONSOLE
Run Code Online (Sandbox Code Playgroud)

无论我尝试做什么,我都无法阻止来自Weld的log4j消息显示在控制台中.我想做的就是完全禁用日志记录.但是怎么样?

axi*_*sty 13

在发布这个问题后不久,我发现了答案.创建log4j.xml文件以覆盖第三方JAR中的默认值.

以下是此线程的特定示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>

    <appender name="CA" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%m%n"/>
        </layout>
    </appender>

    <!-- 
        If you want to enable logging for the application
        but wish to disable logging for a specific package
        then use this, where org.jboss is the package
        for which you wish to disable logging.
    -->
    <category name="org.jboss">
        <priority value="off"/>
    </category>

    <root>
        <priority value="off"/> <!--Notice this disables all logging-->
        <appender-ref ref="CA"/>
    </root>

</log4j:configuration>
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用。非常感谢。只是提醒把文件放在源目录下。 (2认同)