简体   繁体   English

停止来自第三方 Jars 的 log4j 调试打印

[英]Stop log4j debug printing from third party Jars

I'm using classes from other Jars which use classes from other Jars that use log4j.我正在使用来自其他 Jars 的类,这些类使用来自使用 log4j 的其他 Jars 的类。 I cannot change any of these Jars.我不能改变这些罐子中的任何一个。

A lot of these classes are printing debug to the console at startup when debugging in IntelliJ.在 IntelliJ 中调试时,许多这些类在启动时将调试打印到控制台。

I'm looking for ways to stop this as it's irritating.我正在寻找方法来阻止它,因为它很烦人。

I've tried creating a log4j.properties file with "error" log level and had it load in the main() of my application as well as setting the default logger to "error" level.我已经尝试创建一个具有“错误”日志级别的 log4j.properties 文件,并将其加载到我的应用程序的 main() 中,并将默认记录器设置为“错误”级别。 I also checked and all the Jars have a properties inside but non are specfying what log level (they just say what version the Jar is).我还检查了所有的 Jars 里面都有一个属性,但没有指定什么日志级别(他们只是说 Jar 是什么版本)。 I've tried creating my own properties files for the classes to use which works but they've already printed their debug before they load the file.我已经尝试为要使用的类创建自己的属性文件,但它们在加载文件之前已经打印了调试。

Here's some of the debug chatter being produced:以下是一些正在产生的调试信息:

12:54:17.660 [main] DEBUG org.apache.commons.configuration.PropertiesConfiguration - FileName set to xxx.properties
12:54:17.665 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is xxx.properties
12:54:17.665 [main] DEBUG org.apache.commons.configuration.DefaultFileSystem - Could not locate file xxx.properties at null: no protocol: xxx.properties
12:54:17.668 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - Loading configuration from the path xxx.properties
12:54:17.668 [main] DEBUG org.apache.commons.configuration.PropertiesConfiguration - Base path set to file:///C:/myProgram/xxx.properties
12:54:17.680 [main] DEBUG org.apache.commons.configuration.PropertiesConfiguration - FileName set to yyy.properties
12:54:17.680 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is yyy.properties
12:54:17.681 [main] DEBUG org.apache.commons.configuration.DefaultFileSystem - Could not locate file yyy.properties at null: no protocol: yyy.properties
12:54:17.681 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - Loading configuration from the path yyy.properties
12:54:17.681 [main] DEBUG org.apache.commons.configuration.PropertiesConfiguration - Base path set to file:///C:/myProgram/yyy.properties

Here are my .properties files:这是我的 .properties 文件:

---log4j.properties--- ---log4j.properties---

log4j.rootLogger=error

---xxx.properties--- ---xxx.properties---

log4j.rootLogger=error

---yyy.properties--- ---yyy.properties---

log4j.rootLogger=error

my main method starts like this:我的主要方法是这样开始的:

 public static void main(String[] args) {
        PropertyConfigurator.configure("log4j.properties");
        Logger.getRootLogger().setLevel(Level.ERROR);

The log4j.properties file is being loaded fine with no issue, and is happy with "log4j.rootLogger=error". log4j.properties 文件加载正常,没有问题,并且对“log4j.rootLogger=error”感到满意。 (if I add ', R' or ', stdout' like I've seen in other SO questions it complains but I suspect that is due to me being on Windows ). (如果我添加 ', R' 或 ', stdout' ,就像我在其他 SO 问题中看到的那样,它会抱怨,但我怀疑这是由于我在 Windows 上)。

If this isn't possible, let me know and I'll just accept it, live with it and stop looking.如果这是不可能的,请告诉我,我会接受它,接受它并停止寻找。

This project is run by a large audience using windows, mac and linux.该项目由大量使用 windows、mac 和 linux 的用户运行。 (the project is a test harness for developers who need to run the collection of Jars do develop new ones for the collection) (该项目是一个测试工具,供需要运行 Jars 集合的开发人员为该集合开发新的)

--- edit ---- - - 编辑 - -

I've investigated the suggestion of putting a log4j.xml in my project's src/main/resource folder but can't get it to work.我调查了将 log4j.xml 放在我项目的 src/main/resource 文件夹中的建议,但无法让它工作。

Everything I read indicates the log4j.xml should work but I'm running out of things to try.我读到的所有内容都表明 log4j.xml 应该可以工作,但我没有什么可以尝试的了。 As a last ditch attempt I tried to google: "org.apache.commons.configuration" debug stop and google sent me to my own question haha作为最后一次尝试,我尝试谷歌:“org.apache.commons.configuration”调试停止,谷歌将我发送到我自己的问题哈哈

Here's my log4j.xml which doesn't seem to be stopping the org.apache.commons.configuration from each jar debug printing every time those classes are initilised (which happens a few hundred times a second).这是我的 log4j.xml,每次这些类被初始化(每秒发生几百次)时,它似乎并没有停止每个 jar 调试打印中的 org.apache.commons.configuration。

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

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

    <logger name="org.apache.commons.configuration">
        <level value="error"/>
    </logger>

    <root>
        <priority value="off"/>
        <appender-ref ref="CA"/>
    </root>

</log4j:configuration>

I've rebuilt my project and repackaged my jar.我重建了我的项目并重新打包了我的 jar。 I'm hoping I don't have to put the log4j.xml into each Jar's resources folder?我希望不必将 log4j.xml 放入每个 Jar 的资源文件夹中? :/ :/

Is there a way to see if it's loading the log4j.xml file?有没有办法查看它是否正在加载 log4j.xml 文件?

Is there something I need to do to make all the Jars which are presumably instantiting their own instances of log4j use this log4j.xml?我需要做些什么才能使所有可能实例化自己的 log4j 实例的 Jars 都使用这个 log4j.xml?

I tried this now for my main method:我现在为我的主要方法尝试了这个:

    public static void main(String[] args) {
        PropertyConfigurator.configure("log4j.xml");
        Logger.getRootLogger().setLevel(Level.ERROR);

but I am still seeing the console spammed with DEBUG messages from org.apache.commons.configuration但我仍然看到控制台中充斥着来自 org.apache.commons.configuration 的 DEBUG 消息

-- edit 2 -- -- 编辑 2 --

Still stuck.还是卡住了。 I tried simplying to a really basic example and DEBUG output is still appearing in the console.我试着简单地看一个非常基本的例子,调试输出仍然出现在控制台中。

Here is my simple main method:这是我的简单主要方法:

public static void main(String[] args) throws ConfigurationException {
  PropertyConfigurator.configure("log4j.xml");
  Logger.getRootLogger().setLevel(Level.ERROR);
  PropertiesConfiguration config = new PropertiesConfiguration("log4j.xml");
}

Here are the imports:以下是进口:

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

3rd line causes this output to appear:第 3 行导致出现此输出:

10:04:08.526 [main] DEBUG org.apache.commons.configuration.PropertiesConfiguration - FileName set to log4j.xml
10:04:08.536 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is log4j.xml
10:04:08.536 [main] DEBUG org.apache.commons.configuration.DefaultFileSystem - Could not locate file log4j.xml at null: no protocol: log4j.xml
10:04:08.546 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - Loading configuration from the path log4j.xml

Here is the log4j.xml which I confirmed is being loaded by renaming it and seeing a load file error.这是我通过重命名并看到加载文件错误来确认正在加载的 log4j.xml。

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

    <root>
        <level value="error"/>
        <priority value="off"/> 
    </root>

    <logger name="org.springframework">
        <level value="error"/>
        <priority value="off"/>
    </logger>

    <logger name="org.apache.commons.configuration">
        <level value="error"/>
        <priority value="off"/>
    </logger>


    <logger name="org.apache.commons.configuration.PropertiesConfiguration">
        <level value="error"/>
        <priority value="off"/>
    </logger>

    <logger name="org.apache.commons">
        <level value="error"/>
        <priority value="off"/>
    </logger>


    <logger name="org.apache">
        <level value="error"/>
        <priority value="off"/>
    </logger>


</log4j:configuration>

I created a file called "logback-test.xml" and tried placing it in every possible folder I could think of, finally found if I put it into here "Log4JTest\\out\\production\\Log4JTest" (where Log4JTest is the name of my test IntelliJ project) then it is found.我创建了一个名为“logback-test.xml”的文件并尝试将它放在我能想到的每个可能的文件夹中,最后发现如果我把它放在这里“Log4JTest\\out\\production\\Log4JTest”(其中 Log4JTest 是我的名称)测试 IntelliJ 项目)然后找到它。

The loopback-test.xml has the following contents: loopback-test.xml 的内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <include resource="org/springframework/boot/logging/logback/base.xml" />
        <logger name="org.apache" level="OFF"/>
    </configuration>

and it seems to have done the job!它似乎已经完成了工作!

Here's my Main.java这是我的 Main.java

package com.company;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class Main {

    public static void main(String[] args) throws ConfigurationException {
        PropertiesConfiguration config = new PropertiesConfiguration("log4j.xml");
    }
}

and here's the output console:这是输出控制台:

Connected to the target VM, address: '127.0.0.1:50725', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:50725', transport: 'socket'

Process finished with exit code 0

Now to test on the full project... will mark this as 'solved' if it solves the original problem.现在测试整个项目......如果它解决了原始问题,将把它标记为“已解决”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM