Categories
log4j

How to initialize log4j properly?

306

After adding log4j to my application I get the following output every time I execute my application:

log4j:WARN No appenders could be found for logger (slideselector.facedata.FaceDataParser).
log4j:WARN Please initialize the log4j system properly.

It seems this means a configuration file is missing.
Where should this config file be located and what is a good start content?

I’m using plain java for developing a desktop application. So no webserver etc…

3

300

Log4j by default looks for a file called log4j.properties or log4j.xml on the classpath.

You can control which file it uses to initialize itself by setting system properties as described here (Look for the “Default Initialization Procedure” section).

For example:

java -Dlog4j.configuration=customName ....

Will cause log4j to look for a file called customName on the classpath.

If you are having problems I find it helpful to turn on the log4j.debug:

-Dlog4j.debug

It will print to System.out lots of helpful information about which file it used to initialize itself, which loggers / appenders got configured and how etc.

The configuration file can be a java properties file or an xml file. Here is a sample of the properties file format taken from the log4j intro documentation page:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

5

  • 44

    So for loading the configuartion file from a file which is not on a classpath you have to do it like: -Dlog4j.configuration=file:/c:/my/folder/log4j.properties which is actually a URL.

    – bbcooper

    Jan 3, 2011 at 10:10

  • 15

    one small tip that maybe someone will find useful: you can also turn on the log4j debugger by enabling the corresponding property in code – System.setProperty(“log4j.debug”, “”);

    – XXL

    May 19, 2013 at 13:29


  • Where do you put java -Dlog4j.configuration=customName? I tried Project / Preferences / Run/Debug Settings , picked some configurations, clicked Edit, Arguments tab, VM arguments. Does the customName include a .xml extension?

    – Noumenon

    Aug 20, 2015 at 2:22


  • 2

    After trying many variations, this worked: -Dlog4j.configuration=file:///C:/mydir/subdir/log4j.properties The example above: -Dlog4j.configuration=file:/c:/my/folder/log4j.properties failed.

    Apr 1, 2016 at 21:11


  • 2

    I really wish manuals would emphasize more that -Dlog4j.debug can help debug your configuration.

    Jan 15, 2019 at 23:33

253

While setting up log4j properly is great for “real” projects you might want a quick-and-dirty solution, e.g. if you’re just testing a new library.

If so a call to the static method

org.apache.log4j.BasicConfigurator.configure();

will setup basic logging to the console, and the error messages will be gone.

4

  • Hi a3. 14_Infinity where i should use this? i can’t understand. even i have referred more answers for this issue. please help me

    Jul 27, 2017 at 10:06

  • 6

    Keep in mind that this will default to Debug-level logging, which may not be desirable. You can change this like so: Logger.getRootLogger().setLevel(Level.INFO);

    Sep 28, 2017 at 21:41

  • Not just did the warnings go away, but I got some handy debug info output in the console window

    Nov 1, 2017 at 13:19

  • 2

    so ironic..we use it on the production state

    – aswzen

    Mar 27, 2018 at 9:11

26

If you just get rid of everything (e.g. if you are in tests)

org.apache.log4j.BasicConfigurator.configure(new NullAppender());