Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j: DailyRollingFileAppender with MaxFileSize Option

Tags:

log4j

I am using this log4j.properties

log4j.rootCategory=Info, A1
# A1 is a DailyRollingFileAppender
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.file=D:/MyWeb.log
log4j.appender.A1.datePattern='.'yyyy-MM-dd
log4j.appender.A1.append=true
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-22d{dd/MMM/yyyy HH:mm:ss} - %m%n

I want to display logs in the Date Wise Order , so I am using DailyRollingFileAppender. But the issue is that this log file currently cannot hold much data (meaning when lot of requests are made on that day) it looses the previous log data

I tried to use the option MaxFileSize:

log4j.appender.A1.MaxFileSize=10MB

But on to the server console its giving error that property MaxFileSize isn't supported .

Please tell me if there is any other way that the log appears date wise and it can hold as much data as specified.

like image 690
Pawan Avatar asked Dec 20 '11 15:12

Pawan


People also ask

What is maxFileSize in log4j?

maxFileSize. The default maximum file size is 10MB. Fields inherited from class org.apache.log4j.FileAppender.

What is rolling file Appender in log4j?

Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.

What is maxBackupIndex in log4j?

maxBackupIndex This property denotes the number of backup files to be created. Default value is 1. Following is a sample configuration file log4j. properties for RollingFileAppender. # Define the root logger with appender file.


2 Answers

You could extend the FileAppender class and implement your custom version. More details DailyRollingFileAppender

like image 73
nayakam Avatar answered Oct 10 '22 09:10

nayakam


You could use DailyRollingFileAppender with the hourly backup option. This will rollover the logs every hour.

Hourly Usage:

log4j.appender.A1.datePattern='.'yyyy-MM-dd-HH
like image 21
Srikar Avatar answered Oct 10 '22 10:10

Srikar