0

I am using log4j to manage logs in my java application.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>
        <appender name="console" class="org.apache.log4j.ConsoleAppender">
            <param name="Target" value="System.out" />
            <param name="Threshold" value="debug" />
            <layout class="org.apache.log4j.PatternLayout">
                 <param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
            </layout>
        </appender>
        <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
            <param name="Threshold" value="INFO" />
            <param name="Threshold" value="DEBUG" />
            <param name="maxFileSize" value="10MB" />
            <param name="maxBackupIndex" value="10" />
            <param name="file" value="C:/test_reports/infoToolsLog.log"/>
            <param name="append" value="true" />
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %m %n" />
            </layout>
        </appender>

     <root>
        <level value="info" />
        <appender-ref ref="fileAppender" />
    </root>

</log4j:configuration>

Test Code:

import org.apache.log4j.Logger;
public class TestLogFile {
    private static final Logger LOGGER = Logger.getLogger(TestLogFile.class);
    public static void main(String args[]){
        System.out.println("In TestLogFIle");
        LOGGER.info("in TestLogFile, info statement");
        LOGGER.debug("in TestLogFile debug statement");
        LOGGER.error("in TestLogfile, error statement");
        int i=10;
        try {
            int j=i/0;
        }catch(Exception e){
            LOGGER.error("error occured in TestLogfile : " ,e);

        }
    }
}

messages displayed in log file :

2017-06-16 11:34:57 INFO  TestLogFile:10 in TestLogFile, info statement 
2017-06-16 11:34:57 ERROR TestLogFile:12 in TestLogfile, error statement 
2017-06-16 11:34:57 ERROR TestLogFile:18 error occured in TestLogfile :  
java.lang.ArithmeticException: / by zero...

When <level value="info" />, how to show the debug statements also included in log file. What changes need to be done to to show info,error and debug statements printed in the log file when <level value="info" />

Ran
  • 11
  • 1
  • 5

2 Answers2

3

See the different types of log levels here:

https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html

This image can help you understand it better:enter image description here

You have to use DEBUG to show FATAL, ERROR, WARN, INFO and DEBUG

<level value="DEBUG" />

Similarly you have to use TRACE to show FATAL, ERROR, WARN, INFO, DEBUG and TRACE

<level value="TRACE" />
NEO
  • 1,961
  • 8
  • 34
  • 53
  • @NEO- Appreciate your help. Please see the table mentioned in https://stackoverflow.com/questions/7745885/log4j-logging-hierarchy-order. thanks – Ran Jun 16 '17 at 15:52
  • @NEO- Thanks, its very clear.Can you please share the link from where i can see the table you mentioned above. – Ran Jun 16 '17 at 16:10
1

Change

<level value="info" />

to

<level value="debug" />

If you set the level to info then you filter out messages that are lower (trace and debug). Since you want to see the debug messages, you need to set the level to debug.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • When we give level value="info" , debug messages also should get printed in the log file.Do we have any level configuration to achieve this? – Ran Jun 16 '17 at 15:47
  • @Ran Read my answer again. You are filtering out debug and trace messages when you set the level to info. You set the level to debug, or change your debug calls to info. – Elliott Frisch Jun 16 '17 at 15:48