java 如何在 3rd 方 jar 中禁用 log4j?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2732981/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:36:04  来源:igfitidea点击:

How to disable log4j in 3rd party jar?

javalog4j

提问by axiopisty

I'm experimenting writing a java SE swing application using JBoss weld. Weld configures logging with log4j using the following log4j.xml file in the jar:

我正在尝试使用 JBoss 焊接编写 java SE 摆动应用程序。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>

I want to disable logging altogether in my application. I tried to disable it providing a log4j.properties file as follows:

我想在我的应用程序中完全禁用日志记录。我试图禁用它提供一个 log4j.properties 文件如下:

log4j.debug=FALSE
log4j.rootLogger=OFF, CONSOLE

No matter what I try to do I cannot prevent the log4j messages from Weld to show up in the console. All I want to do is completely disable logging. But How?

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

采纳答案by axiopisty

Shortly after posting this question I discovered the answer. Create a log4j.xmlfile to override the defaults in the 3rd party JAR.

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

Here is an example specific to this thread:

这是特定于该线程的示例:

<?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>

回答by suiwenfeng

I think the default log configuration for third part jars won`t print any message into STDOUT or FILE.

我认为第三方 jar 的默认日志配置不会将任何消息打印到 STDOUT 或 FILE。

But if you want override the default log configuration without document, the best solution is de-compile the jar files and find out how it load the configuration.

但是如果你想在没有文档的情况下覆盖默认的日志配置,最好的解决方案是反编译 jar 文件并找出它如何加载配置。

回答by Ted Beckett

If a log4j xml-format configuration file is present, it takes precedence over property files. That is log4j defined behavior. So you can put your configuration in log4j.xml and it should take effect.

如果存在 log4j xml 格式配置文件,则它优先于属性文件。那是 log4j 定义的行为。所以你可以把你的配置放在 log4j.xml 中,它应该会生效。