在 Java 中禁用 Log4J 输出

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

Disabling Log4J Output in Java

javalogginglog4j

提问by cmcginty

How can one quickly turn off all Log4Joutput using a log4j.propertiesfile?

如何使用文件快速关闭所有Log4J输出log4j.properties

采纳答案by Edwin

Set level to OFF (instead of DEBUG, INFO, ....)

将级别设置为 OFF(而不是 DEBUG、INFO、....)

回答by Rolf

You can change the level to OFF which should get rid of all logging. According to the log4j website, valid levels in order of importance are TRACE, DEBUG, INFO, WARN, ERROR, FATAL. There is one undocumented levelcalled OFF which is a higher level than FATAL, and turns off all logging.

您可以将级别更改为 OFF,这将消除所有日志记录。根据 log4j 网站,按重要性顺序排列的有效级别是 TRACE、DEBUG、INFO、WARN、ERROR、FATAL。有一个称为 OFF 的未记录级别,它比 FATAL 级别更高,并关闭所有日志记录。

You can also create an extra root logger to log nothing (level OFF), so that you can switch root loggers easily. Here's a postto get you started on that.

您还可以创建一个额外的根记录器来不记录任何内容(级别关闭),以便您可以轻松切换根记录器。这是一篇文章,可以帮助您开始。

You might also want to read the Log4J FAQ,because I think turning off all logging may not help. It will certainly not speed up your app that much, because logging code is executed anyway, up to the point where log4j decides that it doesn't need to log this entry.

您可能还想阅读Log4J FAQ,因为我认为关闭所有日志记录可能无济于事。它肯定不会大大加快您的应用程序的速度,因为无论如何都会执行记录代码,直到 log4j 决定不需要记录此条目为止。

回答by flybywire

 log4j.rootLogger=OFF

回答by Andrew Gilmartin

If you want to turn off logging programmatically then use

如果要以编程方式关闭日志记录,请使用

List<Logger> loggers = Collections.<Logger>list(LogManager.getCurrentLoggers());
loggers.add(LogManager.getRootLogger());
for ( Logger logger : loggers ) {
    logger.setLevel(Level.OFF);
}

回答by Do Nhu Vy

Change level to what you want. (I am using Log4j2, version 2.6.2). This is simplest way, change to <Root level="off">

将级别更改为您想要的。(我使用的是 Log4j2,版本 2.6.2)。这是最简单的方法,更改为<Root level="off">

For example: File log4j2.xml
Development environment

例如:文件log4j2.xml
开发环境

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <Console name="SimpleConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="SimpleConsole"/>
        </Root>
    </Loggers>
</Configuration>

Production environment

生产环境

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <Console name="SimpleConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="off">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
    <Loggers>
        <Root level="off">
            <AppenderRef ref="SimpleConsole"/>
        </Root>
    </Loggers>
</Configuration>

回答by maytham-???????

In addition, it is also possible to turn logging off programmatically:

此外,还可以通过编程方式关闭注销:

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

Or

或者

Logger.getRootLogger().removeAllAppenders();
Logger.getRootLogger().addAppender(new NullAppender());

These use imports:

这些使用进口:

import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.NullAppender;