Tushar Ahirrao
Tushar Ahirrao

Reputation: 13115

Turn off debug or log messages in log4j.xml

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

My log4j.xml is :

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

 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

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.

Upvotes: 9

Views: 45712

Answers (5)

Joman68
Joman68

Reputation: 2850

You can set the logging level for a package like this:

log4j2.xml snippet:

    <Loggers>
        <Root level="debug">
            <appender-ref ref="Console" />
        </Root>
        <Logger name="org.eclipse.jetty" level="info" additivity="true">
            <appender-ref ref="Console" />
        </Logger>
    </Loggers>

In this example, classes in the org.eclipse.jetty package output INFO level logging. All other classes output DEBUG level logging.

Upvotes: 1

Eliko
Eliko

Reputation: 1

We should write to the log4j config file

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

Upvotes: 2

Paul Vargas
Paul Vargas

Reputation: 42010

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.

Upvotes: 6

Santhosh
Santhosh

Reputation: 8187

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,

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

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

Upvotes: 0

Abhinav kumar
Abhinav kumar

Reputation: 116

set the logging level Value = OFF instead of DEBUG

Upvotes: 4

Related Questions