📅  最后修改于: 2020-11-12 05:52:08             🧑  作者: Mango
要将日志记录信息写入文件,您必须使用org.apache.log4j.FileAppender 。
FileAppender具有以下可配置参数:
Property | Description |
---|---|
immediateFlush | This flag is by default set to true, which means the output stream to the file being flushed with each append operation. |
encoding | It is possible to use any character-encoding. By default, it is the platform-specific encoding scheme. |
threshold | The threshold level for this appender. |
Filename | The name of the log file. |
fileAppend | This is by default set to true, which means the logging information being appended to the end of the same file. |
bufferedIO | This flag indicates whether we need buffered writing enabled. By default, it is set to false. |
bufferSize | If buffered I/O is enabled, it indicates the buffer size. By default, it is set to 8kb. |
以下是FileAppender的示例配置文件log4j.properties-
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=${log}/log.out
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
如果您希望拥有一个与上述log4j.properties文件等效的XML配置文件,那么这里是内容:
您可以尝试使用上述配置的log4j-示例程序。
出于某些原因,例如,如果文件大小达到某个阈值,您可能希望将日志消息写入多个文件。
要将日志记录信息写入多个文件,必须使用org.apache.log4j.RollingFileAppender类,该类扩展了FileAppender类并继承了其所有属性。
除了上面提到的FileAppender以外,我们还有以下可配置参数-
Property | Description |
---|---|
maxFileSize | This is the critical size of the file above which the file will be rolled. Default value is 10 MB. |
maxBackupIndex | This property denotes the number of backup files to be created. Default value is 1. |
以下是RollingFileAppender的示例配置文件log4j.properties 。
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
# Set the name of the file
log4j.appender.FILE.File=${log}/log.out
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true
# Set the maximum file size before rollover
log4j.appender.FILE.MaxFileSize=5MB
# Set the the backup index
log4j.appender.FILE.MaxBackupIndex=2
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
如果希望拥有XML配置文件,则可以生成与初始部分中所述相同的文件,并仅添加与RollingFileAppender相关的其他参数。
此示例配置说明每个日志文件的最大允许大小为5 MB。超过最大大小后,将创建一个新的日志文件。由于maxBackupIndex定义为2,一旦第二个日志文件达到最大大小,则将删除第一个日志文件,然后,所有日志记录信息将回滚到第一个日志文件。
您可以尝试使用上述配置的log4j-示例程序。
可能需要每天生成日志文件,以保持日志信息的干净记录。
要将日志记录信息每天写入文件,您必须使用org.apache.log4j.DailyRollingFileAppender类,该类扩展了FileAppender类并继承了其所有属性。
除了上面提到的FileAppender之外,只有一个重要的可配置参数:
Property | Description |
---|---|
DatePattern | This indicates when to roll over the file and the naming convention to be followed. By default, roll over is performed at midnight each day. |
DatePattern使用以下模式之一控制过渡计划:
DatePattern | Description |
---|---|
‘.’ yyyy-MM | Roll over at the end of each month and at the beginning of the next month. |
‘.’ yyyy-MM-dd | Roll over at midnight each day. This is the default value. |
‘.’ 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. |
以下是一个示例配置文件log4j.properties,用于生成在每天的中午和午夜滚动的日志文件。
# 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 immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true
# Set the DatePattern
log4j.appender.FILE.DatePattern='.' yyyy-MM-dd-a
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
如果希望拥有XML配置文件,则可以生成与初始部分中所述相同的文件,并仅添加与DailyRollingFileAppender相关的其他参数。
您可以尝试使用上述配置的log4j-示例程序。