Set up log4j.properties and start logging
This log4j.properties example is from one of my projects:
#### Reference ####
# - List of logging levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL
#### Production logging
#log4j.rootLogger=TRACE, dailyfile
#### Development logging
log4j.rootLogger=INFO, fileAppender
#log4j.rootLogger=TRACE, stdout, fileAppender
#### User specific logging
#log4j.rootLogger=TRACE, stdout, fileAppender, dailyfile, rollingfile
### Set log level to packages or classes
#log4j.logger.xpnavigator.utils=TRACE
### Console - stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%r; %d; [%t]; %p; %c; %x; - %m%n
### File - fileAppender
log4j.appender.fileAppender=org.apache.log4j.FileAppender
log4j.appender.fileAppender.File=./XPN.log
log4j.appender.fileAppender.Append=false
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%r; %d; [%t]; %p; %c; %x; - %m%n
### File - dailyfile
log4j.appender.dailyfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyfile.File=./XPN.log
log4j.appender.dailyfile.DatePattern='.'yyyy-MM-dd
log4j.appender.dailyfile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyfile.layout.ConversionPattern=%r; %d; [%t]; %p; %c; %x; - %m%n
### File - rollingfile (change maxFileSize and maxBackupIndex parameters)
log4j.appender.rollingfile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingfile.File=./XPN.log
log4j.appender.rollingfile.maxFileSize=10MB
log4j.appender.rollingfile.maxBackupIndex=5
log4j.appender.rollingfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingfile.layout.ConversionPattern=%r; %d; [%t]; %p; %c; %x; - %m%n
Comments
Lines starting with # are comments.
Logging levels
log4j.rootLogger - set logging level and set handling of log file (there are several examples)
Logging levels:
TRACE < DEBUG < INFO < WARN < ERROR < FATAL
Setting logging level to INFO means only outputs with logging level equal or greater than INFO will be logged.
Log file output
stdout - standard output, log to console (use for
development, this logger goes to Eclipse console)
fileAppender - append new data to existing logger if
Append=true, otherwise start new logger each time
dailyfile - start new log file every day
rollingfile - when log file size reaches maxFileSize new
logger is opened
Log file filename
Set location and filename for logger with parameter File=./path/filename.log
Setup log4j in Eclipse
In your Eclipse project create directory ‘cfg’ and put log4j.properties file in it.
In your project open Run Configuration window and go to Arguments tab. Enter VM arguments:
-Dlog4j.configuration=log4j.properties
Go to Classpath tab and under User entries add ‘cfg’ directory: click Advanced button, select Add External Folder and select ‘cfg’ directory.
Run your application. Make sure that log4j-x.x.xx.jar file is visible in Java Build Path.
Running Java from Console
When running your java application from command line in Console window, you also have to specify correct path to log4j.properties file. This is usually the trickiest part. Otherwise you will get that annoying message saying:
log4j:WARN No appenders could
be found for logger (test.LoggerTestMain).
log4j:WARN Please initialize the log4j system properly.
The path to log4j.properties file must be specified as url address! Be careful how many slashes or backslashes you put in the url.
Url slightly differs for Linux and Windows - remember that Windows OS uses those silly backslashes! The following examples show usage of relative path to log4j.properties file. Besides this is more flexible way of setting file path than setting absolute file path. And avoid using relative paths such as ‘./’ since this is not a valid url.
Linux (example from shell script):
currDir=$(pwd)
java -Dlog4j.configuration=file://$currDir/cfg/log4j.properties -jar ./MyProject.jar
Windows:
java -Dlog4j.configuration=file:///%CD%\cfg\log4j.properties -jar .\MyProject.jar
For debugging of log4j system, add another VM argument:
-Dlog4j.debug=true