Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在第三方jar中禁用log4j?_Java_Log4j - Fatal编程技术网

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

Java 如何在第三方jar中禁用log4j?,java,log4j,Java,Log4j,我正在尝试使用JBoss weld编写JavaSE 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

我正在尝试使用JBoss weld编写JavaSE 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>

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

如果存在log4jxml格式的配置文件,它将优先于属性文件。这就是log4j定义的行为。因此,您可以将配置放在log4j.xml中,它应该会生效。

发布此问题后不久,我发现了答案。创建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>

我认为第三方JAR的默认日志配置不会将任何消息打印到标准输出或文件中


但是,如果您想在不使用文档的情况下覆盖默认日志配置,最好的解决方案是反编译jar文件,并了解如何加载配置。

谢谢您的评论。我在写了这个问题后不久就发现了答案。作为回答这个问题的一个具体示例,我将包含一个示例log4j.xml文件,该文件将禁用第三方jboss weld jar中的日志记录。我尝试将覆盖的log4j.xml添加到src/test/resources中,但它看起来被忽略了。我仍然得到那些
信息:WELD-000900 1.1.4(最终版)
。有什么想法吗?你试过把它放在src/main/resources中吗?这对我很有用。谢谢。只需将文件放在源目录下。
<?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>