To write log information into a file, you must use Org.apache.log4j.FileAppender. The following Fileappender configuration parameters are available:
Fileappender configuration:
Properties |
Description |
Immediateflush |
The default setting for the flag is true, which means that the output stream's files are refreshed at each append operation |
Encoding |
It can use any character encoding. Platform-specific encoding scheme by default |
Threshold |
This appender threshold level |
Filename |
The name of the log file |
Fileappend |
The default setting is true, which means that the logged information is appended to the end of the same file |
Bufferedio |
This flag indicates whether write cache is required to be enabled. The default setting is False |
BufferSize |
If BUFFEREDI/O is enabled, this indicates the size of the buffer, which is set to 8KB by default |
The following is a sample configuration file Log4j.properties Fileappender.
# Define The root logger with Appender Filelog4j.rootlogger=DEBUG, file# Define the FILE appenderlog4j.appender.FILE=org.apache.log4j.fileappender# Set The name of the Filelog4j.appender.FILE.File=${log}/log.out# Set The immediate flush totrue(default) Log4j.appender.FILE.ImmediateFlush=true# Set The threshold to debug Modelog4j.appender.FILE.Threshold=debug# Set the append tofalse, Overwritelog4j.appender.FILE.Append=false# Define The layout forfile Appenderlog4j.appender.FILE.layout=Org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.conversionPattern=%m%n
If you like an XML configuration file equivalent to the above Log4j.properties file, here is the content of the XML configuration file:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "Log4j.dtd"><log4j:configuration><Appendername= "FILE"class= "Org.apache.log4j.FileAppender"> <paramname= "File"value= "${log}/log.out"/> <paramname= "Immediateflush"value= "true"/> <paramname= "threshold"value= "Debug"/> <paramname= "Append"value= "false"/> <Layoutclass= "Org.apache.log4j.PatternLayout"> <paramname= "Conversionpattern"value= "%m%n"/> </Layout></Appender><Loggername= "Log4j.rootlogger"additivity= "false"> < Levelvalue= "DEBUG"/> <Appender-refref= "FILE"/></Logger></log4j:configuration>
You can try the configuration on the log4j-sample program using the above.
Logging to multiple files:
When you want to write log information to convert multiple file requirements, for example, if the file size reaches a certain threshold, and so on.
The Write logging information is divided into multiple files, the Fileappender class must be extended, and all its properties are inherited by the Useorg.apache.log4j.RollingFileAppender class.
There are the following except for the Fileappender configurable parameters as described above:
Properties |
Description |
MaxFileSize |
The rollback critical dimensions of the above files. The default value is 10MB |
Maxbackupindex |
This property represents the number of backup files to be created. The default value is 1 |
The following is a sample configuration file for Log4j.properties Rollingfileappender
# Define The root logger with Appender Filelog4j.rootlogger=DEBUG, file# Define the FILE appenderlog4j.appender.FILE=org.apache.log4j.rollingfileappender# Set The name of the Filelog4j.appender.FILE.File=${log}/log.out# Set The immediate flush totrue(default) Log4j.appender.FILE.ImmediateFlush=true# Set The threshold to debug Modelog4j.appender.FILE.Threshold=debug# Set the append tofalse, should not overwritelog4j.appender.FILE.Append=true# Set The maximum file size before rolloverlog4j.appender.FILE.MaxFileSize=5kb# Set the the The backup Indexlog4j.appender.FILE.MaxBackupIndex=2# Define The layout forfile Appenderlog4j.appender.FILE.layout=Org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.conversionPattern=%m%n
If you want to have an XML configuration file, you can generate the initial segment mentioned in and add the associated Rollingfileappender for the unique additional parameters.
This sample configuration shows that the maximum allowable size for each log file is 5MB. When the maximum size is exceeded, the new log file is created and because Maxbackupindex is defined as 2, when the second log file reaches the maximum value, the first log file is deleted, and all log information is rolled back to the first log file.
You can try Log4j-the sample program uses the above configuration.
Generate Log files daily:
When you want to generate log files for each day, keep a good record of logging information.
When logging information is incorporated into a daily base file, it must extend the Fileappender class and inherit all its properties from the Useorg.apache.log4j.DailyRollingFileAppender class.
There is only one important following configuration parameter except as described above for Fileappender:
| Property
Description |
Datepattern |
This means that the files are scrolled and executed according to the naming convention. By default, scrolls at midnight every day |
Datepattern control uses one of the following scrolling schedule methods:
Datepattern |
Description |
'. ' Yyyy-mm |
Scroll at the end of each month and next month |
'. ' Yyyy-mm-dd |
This is the default value, which scrolls every day at midnight |
'. ' Yyyy-mm-dd-a |
Scroll every day at midnight and noon |
'. ' Yyyy-mm-dd-hh |
Scroll in every one hours |
'. ' Yyyy-mm-dd-hh-mm |
Rolling in every Minute |
'. ' Yyyy-ww |
The first day of the week depending on the regional setting is scrolled |
The following is a sample configuration file that log4j.properties generate log files for scrolling at midnight every day.
# Define The root logger with Appender Filelog4j.rootlogger=DEBUG, file# Define the FILE appenderlog4j.appender.FILE=org.apache.log4j.dailyrollingfileappender# Set The name of the Filelog4j.appender.FILE.File=${log}/log.out# Set The immediate flush totrue(default) Log4j.appender.FILE.ImmediateFlush=true# Set The threshold to debug Modelog4j.appender.FILE.Threshold=debug# Set the append tofalse, should not overwritelog4j.appender.FILE.Append=true# Set The DatePatternlog4j.appender.FILE.DatePattern= '. ' yyyy-mm-dd-a# Define the layout forfile Appenderlog4j.appender.FILE.layout=Org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.conversionPattern=%m%n
If you want to use an XML configuration file, you can generate the initial segment mentioned in and add the relevant Dailyrollingfileappender unique additional parameters and data.
You can try the configuration on the log4j-sample program using the above.
log4j Tutorial 11, logging to a file