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…
Log4j by default looks for a file called
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).
java -Dlog4j.configuration=customName ....
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:
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
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
will setup basic logging to the console, and the error messages will be gone.
If you just get rid of everything (e.g. if you are in tests)