Log4j 1.2.17 - How to do log rolling based on size - log4j

I would like my logfile to roll when it reaches to a given size (lets say 100MB) and rolled file should be archived(.gz) Also, the zip file should have current time stamp in its name.
Is it possible to achieve this using log4j 1.2?

You can define the parameter maxFileSize and maxBackupIndex for a RollingFileAppender
For compression check this answer : https://stackoverflow.com/a/3329513/1811730
This link can help : Rotate & Archiving File On Size
If you can, don't use log4j 1.2 but see for log4j 2

Related

Dailyrollingfileappender not creating backup of more than 1 day

My log config is as follows,but i can see only 1 backup file being created and all the other backup just goes off / deleted.
log4j.appender.application=org.apache.log4j.DailyRollingFileAppender
log4j.appender.application.File=${log.root.path}/test.log
log4j.appender.application.DatePattern='.'yyyy-MM-dd
log4j.appender.application.layout=org.apache.log4j.PatternLayout
log4j.appender.application.layout.ConversionPattern=%d{dd MMM
HH\:mm\:ss\:SSS} [%t] %-5p [%c\:%M\:%L] srcIp=%X{srcIp} -
remoteHost=%X{remoteHost} -port=%X{port} - activityType=%X{activitytype} -
activityStatus=%X{activitystatus} -applicationUser=%X{applicationUser} -
APITXNID=%X{apiTxnId} -CustomerRefNum=%X{CustomerRefNum} -%m%n%n
Your configuration looks fine, there are several thing you could try to do:
Try replace log4j.appender.application.DatePattern from '.'yyyy-MM-dd to '.'yyyy-MM-dd-HH-mm and see if files would be created every minute. If it works, chances are that some external cleanup script deletes old logs.
Add -Dlog4jdebug.true to VM options to verify what is effective log4j conf file is used.
Also consider using API Documentation warning
DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender.
You can have the same effect as your config above, by replacing log4j dependency with apache-log4j-extras, which implements log4j 1.2.x, but has extra features, such as org.apache.log4j.rolling.RollingFileAppender with TimeBasedRollingPolicy
Then replace in your log4j.properties
log4j.appender.application=org.apache.log4j.DailyRollingFileAppender
log4j.appender.application.File=${log.root.path}/test.log
log4j.appender.application.DatePattern='.'yyyy-MM-dd
with
log4j.appender.application=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.application.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.application.rollingPolicy.FileNamePattern=${log.root.path}/test.log.%d{yyyy-MM-dd-HH-mm}
log4j.appender.application.File=${log.root.path}/test.log

NLOG Rollover Configuration

Is there a configuration in NLOG that accomplishes the following
1)A new log file should be created , when the current file exceeds a particular size for ex:- 5 MB
2)The old log files should be deleted after a configured amount of time period like for ex: - 1 day
You can find answer for your question (and examples) on page:
Size-based file archival - log files can be automatically archived by moving them to another location after reaching certain size and
Time-based file archival - log files can calso be automatically archived based on time
Try to use the second one and change log files every days. Than you can keep maximum number of archived files.

How can i configure Log4j to generate new log file every time the java application runs

How can I configure Log4j to generate new log file every time the java application runs?
I want the log name to vary every time like [Log file name]_[CurrentTime].log
I am using .property file to configure log4j
i am using RollingFileAppender but this did not worked for me.here is my configuration
log4j.appender.dest4=org.apache.log4j.RollingFileAppender
log4j.appender.dest4.File=.\\\\log4j\\\\user.log
log4j.appender.dest4.layout=org.apache.log4j.PatternLayout
log4j.appender.dest4.layout.ConversionPattern=[%d{dd/MMM/yyyy HH:mm:ss}] : %-5p: %m%n
log4j.appender.dest4.DatePattern='.'yyyy-MM
You might want to take a look at the RollingFileAppender
HereĀ“s an interesting post about the subject which applies to log4j as well: How do I configure a RollingFileAppender to roll by date and size with log4net?
Hope it helps.
You can set FileAppender dynamically
SimpleLayout layout = new SimpleLayout();
FileAppender appender = new FileAppender(layout,"logname."+new Date().toLocaleString(),false);
logger.addAppender(appender);

Log4j: DailyRollingFileAppender with MaxFileSize Option

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.
You could extend the FileAppender class and implement your custom version. More details
DailyRollingFileAppender
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
Use the RollingFileAppender, you are using the wrong Appender!

log4j, fileappender and tomcat6 logs question

I have seen many questions about the above topics but none that address this.
I am trying to use log4j to log to a custom file in $CATALINA_BASE/logs/ directory.
I configured the log4j.xml file and copied it into the $CATALINA_BASE/lib/ directory.
I use the following lines to create the logger -
PatternLayout layout = new PatternLayout();
FileAppender appender= new FileAppender("filename.txt");
This is where my problem is. How do I make the FileAppender take the file name I configured in the log4j.xml?
I was hoping it will automatically pick that up, but there is no consructor for FileAppender that will not take a filename.
Do I have to read the log4j.xml to get the name of the file? If so why in the world do I need to set that property in the xml at all?
Any help would be greatly appreciated.
Thanks,
- Vas
Hmm... why are you creating a FileAppender in code? Just create a logger using LoggerFactory specifying the string (usually in com.xxx.yyy format that you configured in the log4j XML/properties file) and start logging. Make sure the logger is configured to use the FileAppender implementation (Daily or RollingFile) in the config file and you are all set to go.

Resources