关闭log4j.xml中的调试或日志消息

时间:2021-06-09 21:54:50

I want to turn off debug or log message in log4j.xml.

我想在log4j.xml中关闭调试或日志消息。

My log4j.xml is :

我的log4j.xml是:

<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <param name="Threshold" value="debug" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
        </layout>
    </appender>

    <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="false" />
        <param name="file" value="./logs/learning.log" />
        <param name="Threshold" value="debug" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
        </layout>
    </appender>
    <root>
        <level value="DEBUG" />
        <appender-ref ref="consoleAppender" />
        <appender-ref ref="fileAppender" />
    </root>

My Java code :

我的Java代码:

 public static void main(String[] args) {
        log.info("info");
        log.debug("dddffff");
    }

If I run this program still INFO messages are also getting logged, Below is log file

如果我运行此程序仍然会记录INFO消息,下面是日志文件

07 Aug 2014 12:54:49  INFO MainClass - info
07 Aug 2014 12:54:49 DEBUG MainClass - dddffff

Now I want to turn off INFO messages.

现在我想关闭INFO消息。

4 个解决方案

#1


3  

set the logging level Value = OFF instead of DEBUG

设置日志记录级别Value = OFF而不是DEBUG

#2


3  

You can turn off the log for a class or package. e.g.:

您可以关闭类或包的日志。例如。:

<!-- console -->
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <param name="threshold" value="TRACE" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="conversionPattern" 
               value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
    </layout>
</appender>

<!-- categories -->
<category name="org.apache.commons">
    <priority value="OFF" />
</category>
<category name="org.apache.catalina.loader.WebappClassLoader">
    <priority value="OFF" />
</category>

<!-- root -->
<root>
    <priority value="TRACE" />
    <appender-ref ref="STDOUT" />
</root>

See more in Apache log4j 1.2 - Short introduction to log4j.

在Apache log4j 1.2中查看更多内容 - 对log4j的简要介绍。

#3


1  

We should write to the log4j config file

我们应该写入log4j配置文件

<logger name="packageName.Class" additivity="false">
    <level value="INFO" />
    <appender-ref ref="fileAppender" />
</logger>

I used the full qualified name of the class in the config file, which caused to the getLogger method write to the appender file only logs from the specific class. Using the name of the class only you will get in the appender file, all the logs written from other class with the same level or above.

我在配置文件中使用了类的完全限定名,这导致getLogger方法只向特定类写入appender文件。只使用类的名称,您将获得appender文件,从其他类写入的所有日志具有相同级别或更高级别。

#4


0  

Set the threshold value to info, for ex in your configuration file if you need to turn OFF the messages for debug to file appender,

如果您需要将调试消息关闭到文件追加器,请将阈值设置为info,对于配置文件中的ex,

<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="append" value="false" />
    <param name="file" value="./logs/learning.log" />
    <param name="Threshold" value="INFO" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
    </layout>
</appender>

enable logging only for info not for debug.You can turn off logging by setting OFF.

仅对信息而不是调试启用日志记录。您可以通过设置OFF来关闭日志记录。

Read here for more info about configuring the xml file for log4j.

有关配置log4j的xml文件的更多信息,请阅读此处。

#1


3  

set the logging level Value = OFF instead of DEBUG

设置日志记录级别Value = OFF而不是DEBUG

#2


3  

You can turn off the log for a class or package. e.g.:

您可以关闭类或包的日志。例如。:

<!-- console -->
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <param name="threshold" value="TRACE" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="conversionPattern" 
               value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
    </layout>
</appender>

<!-- categories -->
<category name="org.apache.commons">
    <priority value="OFF" />
</category>
<category name="org.apache.catalina.loader.WebappClassLoader">
    <priority value="OFF" />
</category>

<!-- root -->
<root>
    <priority value="TRACE" />
    <appender-ref ref="STDOUT" />
</root>

See more in Apache log4j 1.2 - Short introduction to log4j.

在Apache log4j 1.2中查看更多内容 - 对log4j的简要介绍。

#3


1  

We should write to the log4j config file

我们应该写入log4j配置文件

<logger name="packageName.Class" additivity="false">
    <level value="INFO" />
    <appender-ref ref="fileAppender" />
</logger>

I used the full qualified name of the class in the config file, which caused to the getLogger method write to the appender file only logs from the specific class. Using the name of the class only you will get in the appender file, all the logs written from other class with the same level or above.

我在配置文件中使用了类的完全限定名,这导致getLogger方法只向特定类写入appender文件。只使用类的名称,您将获得appender文件,从其他类写入的所有日志具有相同级别或更高级别。

#4


0  

Set the threshold value to info, for ex in your configuration file if you need to turn OFF the messages for debug to file appender,

如果您需要将调试消息关闭到文件追加器,请将阈值设置为info,对于配置文件中的ex,

<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="append" value="false" />
    <param name="file" value="./logs/learning.log" />
    <param name="Threshold" value="INFO" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
    </layout>
</appender>

enable logging only for info not for debug.You can turn off logging by setting OFF.

仅对信息而不是调试启用日志记录。您可以通过设置OFF来关闭日志记录。

Read here for more info about configuring the xml file for log4j.

有关配置log4j的xml文件的更多信息,请阅读此处。