What is the required configuration in log4j.properties file in order to backup log file in the end of each month (monthly rolling) and continue to have the same name for active log file?
So I found that DatePattern does the trick:
log4j.appender.applog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.applog.encoding=UTF-8
log4j.appender.applog.layout=org.apache.log4j.PatternLayout
log4j.appender.applog.layout.ConversionPattern=%d{yyyyMMdd HH:mm:ss} %m%n
log4j.appender.applog.File=log/process.log
log4j.appender.applog.DatePattern='.'yyyy-MM
Here, name of the active log file is always "process.log" and at the beginning of each month the file is saved with the name "process.log.yyyy-MM" when the first log of that month is pushed to log4j and the active log file is reset.
You can choose the DailyRollingFileAppender and the date pattern configuration. Refer to http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html
and
http://www.tutorialspoint.com/log4j/log4j_logging_files.htm
Related
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
Is there a way to set the fileName prefix for the rolled over file?
I use date for log files, i.e: 21-06-2017.log
And have set the max file size to 10MB.
So when the file size reaches 10MB, rollover is performed and the current file is renamed to 21-06-2017.log.1 and so on...
I have a process which reads the files with extension .log and render it on UI
Also I'm using properties file and change the fileName programatically when my Spring loads.
Any idea how this can be achieved?
[EDIT]
Even if the RolledOver files are named as 21-06-2017_1.log, that would be fine. Also any type of prefix is acceptable, the only end criteria is that the file name should end with ".log"
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
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.
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!