log4j Tutorial - Log4j Log to File








To log information into a file, use org.apache.log4j.FileAppender.

The following table lists the configurable parameters of FileAppender.

PropertyDescription
immediateFlush Default is true. Flush the message for each append operation.
encoding Change the character-encoding. Default to platform-specific encoding scheme.
threshold Threshold level for this appender.
Filename The name of the log file.
fileAppend Default to true. Append logging information to the end of the same file.
bufferedIO Whether to buffer writing. Default to false.
bufferSize If buffered I/O is enabled, set the buffer size. Default is 8kb.




Example

The following is a sample configuration file log4j.properties for FileAppender.

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# file name
log4j.appender.FILE.File=${log}/log.out 

# Set the flush to true
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to true, overwrite
log4j.appender.FILE.Append=true

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n




Log into Multiple Files

We may want to log message into multiple files, for example, if file size reaches to a certain threshold we would like to log message to a new file.

To log information into multiple files, use org.apache.log4j.RollingFileAppender class which extends the FileAppender class and inherits all its properties.

The following table lists configurable parameters in addition to what have been mentioned for FileAppender:

Property Description
maxFileSize Max size of the file which the file will be rolled. Default is 10MB
maxBackupIndex Set the number of backup files to be created. Default value is 1.

The following example shows a sample configuration file log4j.properties for RollingFileAppender.

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
# file name
log4j.appender.FILE.File=${log}/log.out

# Set the maximum file size before rollover
log4j.appender.FILE.MaxFileSize=5KB

# Set the the backup index
log4j.appender.FILE.MaxBackupIndex=2

Daily Log File

To generate log files on daily basis, use org.apache.log4j.DailyRollingFileAppender class which extends the FileAppender class.

DatePattern controls when to roll over the file.

Property Description
DatePattern Indicates when to roll over the file, and the naming convention to be followed. By default roll over at midnight each day.

DatePattern supports the following patterns:

DatePattern Description
'.' yyyy-MM Roll over at the end of each month.
'.' yyyy-MM-dd Default value. Roll over at midnight each day.
'.' yyyy-MM-dd-a Roll over at midday and midnight of each day.
'.' yyyy-MM-dd-HH Roll over at the top of every hour.
'.' yyyy-MM-dd-HH-mm Roll over every minute.
'.' yyyy-ww Roll over on the first day of each week depending upon the locale.

The following code shows a configuration file log4j.properties for rolling over at midday and midnight of each day.

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
# Set the name of the file
log4j.appender.FILE.File=${log}/log.out

# Set the DatePattern
log4j.appender.FILE.DatePattern='.' yyyy-MM-dd-a