67

While trying to execute the following lines only the last two statements are displayed("Here is some ERROR" and "Here is some FATAL") and the first three statements are not displayed.I had just started to learn this topic, can anyone tell why is this happening?

    logger.debug("Here is some DEBUG");
    logger.info("Here is some INFO");
    logger.warn("Here is some WARN");
    logger.error("Here is some ERROR");
    logger.fatal("Here is some FATAL");

the log4j.property has

log4j.rootLogger=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%5p] %d{mm:ss}(%F:%M:%L)%n%m%n%n
6
  • But I have log4j.properties is that not enough? Nov 4, 2009 at 12:37
  • Can you post your log4j.properties file? Nov 4, 2009 at 12:43
  • 2
    you should not name it log4j.property, it should be log4j.properties Nov 4, 2009 at 13:14
  • sorry while editing i made the mistake, it is log4j.properties only Nov 4, 2009 at 13:43
  • 2
    Maybe a stupid question, but have you told your Logger where the property file is? This you do by calling PropertyConfigurator.configure(pathToPropFile);
    – quosoo
    Nov 4, 2009 at 20:01

8 Answers 8

71

You probably have a log4j.properties file somewhere in the project. In that file you can configure which level of debug output you want. See this example:

log4j.rootLogger=info, console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

log4j.logger.com.example=debug

The first line sets the log level for the root logger to "info", i.e. only info, warn, error and fatal will be printed to the console (which is the appender defined a little below that).

The last line sets the logger for com.example.* (if you get your loggers via LogFactory.getLogger(getClass())) will be at debug level, i.e. debug will also be printed.

2
  • 4
    It might also be a log4j.xml file, which is a XML file that is used instead of a Java Properties file. Nov 4, 2009 at 12:42
  • That only works for log4j 1.x . For log4j 2.x, the config syntax is different, you need to define a custom logger logger.example.name=com.example logger.example.level=DEBUG
    – qouteall
    Apr 2 at 4:17
57

Here's a quick one-line hack that I occasionally use to temporarily turn on log4j debug logging in a JUnit test:

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

or if you want to avoid adding imports:

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

The equivalent for log4j2 is:

Logger logger = LogManager.getRootLogger(); 
Configurator.setAllLevels(logger.getName(), Level.getLevel(level))

Thanks to Jeryl Cook

1
  • for log4j2 do this Logger logger = LogManager.getRootLogger(); Configurator.setAllLevels(logger.getName(), Level.getLevel(level));
    – Jeryl Cook
    Apr 18, 2022 at 19:06
11

Put a file named log4j.xml into your classpath. Contents are e.g.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %t %c{1}:%L - %m%n"/>
        </layout>
    </appender>

    <root>
        <level value="debug"/>
        <appender-ref ref="stdout"/>
    </root>

</log4j:configuration>
2
  • 2
    He already has a Properties file. I believe in the latest version of log4j, it's recommended that you use XML instead, but it's not required that you do so - a Properties file can be used. Nov 4, 2009 at 13:45
  • 1
    so what should i do to rectify the problem? Nov 4, 2009 at 14:28
6

This is probably happening because your log4j configuration is set to ERROR. Look for a log4j.properties file with contents like the following:

log4j.rootLogger=ERROR, CONSOLE

# console logging
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p %-20.20t %-24c{1}: %m%n

The rootLogger is set to ERROR level here using a CONSOLE appender.

Note that some appenders like the console appender also have a Threshold property that can be used to overrule the rootLoggers level. You need to check both in this case.

6

If you are coming here because you are using Apache commons logging with log4j and log4j isn't working as you expect then check that you actually have a log4j.jar in your run-time classpath. That one had me puzzled for a little while. I have now configured the runner in my dev environment to include -Dlog4j.debug in the Java command line so I can always see that Log4j is being initialized correctly

1
  • or make sure you're not using slf4j-simple jar and replace with slf4j-log4j12
    – Jason
    Nov 6, 2016 at 1:59
4

You need to set the logger level to the lowest you want to display. For example, if you want to display DEBUG messages, you need to set the logger level to DEBUG.

The Apache log4j manual has a section on Configuration.

1
  • i didnt know what was the problem in the last program, but i copied the same code and executed in a different path and its working now Nov 5, 2009 at 6:47
3

I like to use a rolling file appender to write the logging info to a file. My log4j properties file typically looks something like this. I prefer this way since I like to make package specific logging in case I need varying degrees of logging for different packages. Only one package is mentioned in the example.

log4j.appender.RCS=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RCS.File.DateFormat='.'yyyy-ww
#define output location
log4j.appender.RCS.File=C:temp/logs/MyService.log
#define the file layout
log4j.appender.RCS.layout=org.apache.log4j.PatternLayout
log4j.appender.RCS.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm a} %5 %c{1}: Line#%L - %m%n
log4j.rootLogger=warn
#Define package specific logging
log4j.logger.MyService=debug, RCS
2

This is happening due to the fact that the logging level of your logger is set to 'error' - therefore you will only see error messages or above this level in terms of severity so this is why you also see the 'fatal' message.

If you set the logging level to 'debug' on your logger in your log4j.xml you should see all messages.

Have a look at the log4j introduction for explaination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.