java 如何关闭 log4j 警告?

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

How to turn off log4j warnings?

javalog4j

提问by Jay Sullivan

I'm running a Java app that depends on a few libraries (Axis2) which use log4j. I don't use log4j and don't have any configuration file. I'd like to just completely disable log4j and squelch any and all warnings that it spits out. Right now, when I run my app, when I call one of the library's methods, I see:

我正在运行一个 Java 应用程序,它依赖于一些使用 log4j 的库 (Axis2)。我不使用 log4j,也没有任何配置文件。我想完全禁用 log4j 并消除它发出的任何和所有警告。现在,当我运行我的应用程序时,当我调用库的方法之一时,我看到:

log4j:WARN No appenders could be found for logger 
(org.apache.axis2.deployment.FileSystemConfigurator).
log4j:WARN Please initialize the log4j system properly.

Is there some static log4j disable method I can call? I tried Logger.getRootLogger().removeAllAppenders(); and this did not work (I still see the warnings).

是否有一些我可以调用的静态 log4j 禁用方法?我试过 Logger.getRootLogger().removeAllAppenders(); 这不起作用(我仍然看到警告)。

回答by Stephen P

You could try

你可以试试

Logger.getRootLogger().setLevel(Level.OFF);

回答by stackoverflowuser2010

The Apache libraries are horribly user-unfriendly. If I wanted logging, I would have turned it on myself. To turn the logging off:

Apache 库对用户非常不友好。如果我想记录日志,我会自己打开它。要关闭日志记录:

  1. Add log4j-1.2.xxx.jar to your CLASSPATH.
  2. Then add the following to your Java code before the logging starts:

    org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.OFF);

  1. 将 log4j-1.2.xxx.jar 添加到您的 CLASSPATH。
  2. 然后在日志记录开始之前将以下内容添加到您的 Java 代码中:

    org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.OFF);

回答by Girl Spider

Make sure you import org.apache.log4j.Level, not the java.util.logging.Level, otherwise you get an error saying "The method setLevel(Level) in the type Category is not applicable for the arguments (Level)"

确保您导入 org.apache.log4j.Level,而不是 java.util.logging.Level,否则您会收到错误消息“类型类别中的方法 setLevel(Level) 不适用于参数 (Level)”