Daily Logs Using log4j
Log4j has a file appender which helps you to obtain log file backups In daily basis. This is DailyRollingFileAppender. A sample log4j.properties file is shown below.
log4j.rootLogger= R
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=logs/My_Log.log
log4j.appender.R.DatePattern=’.'yyyy-MM-dd’
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%5p (%F:%L) – %m%n
Date pattern is what is used by the log4j to identify the required backing up pattern. Here basically you can use any valid format in SimpleDateFormatter. Above date pattern will backup the log file in daily basis. A date pattern like log4j.appender.R.DatePattern=’.'yyyy-MM-dd-hh’ will produce backup files in each hour.
When Log4j make backup files
In my original example I have mentioned that the log file will be backed up in daily basis. But this I not entirely true. Ideally log4j should make the backup file at the midnight of each day. But it does not happen in that way.
Before each log operation, log4j will check the last modified date of the log file with the current date. If they are different log4j will rename the original log file as My_Log.log.<Last modified date of the file in given format>. And it will also make a new file named My_Log.log file to insert new logs. So in the backup files list if you saw that log files for some days are missing, do not panic. It is because there were no logs written in those particular dates.
How do I get a lof file as FILE_NAME.log
Generally file names of the backup files are FILE_NAME.log. <Last modified date of the file in given format>. (When you use the DailyRollingFileAppender). So the backup file are not .log files. So you cannot open them directly using a normal textpad. Most of the time you have to explicitely rename those files to .log files. This is tedious and time consuming. But there is a small and simple trick ( or a hack). Log4j directly use the datePattern attribute to generate the file names of the backup files. So log4j.appender.R.DatePattern=’.'yyyy-MM-dd’.log will make the backup file names asĀ FILE_NAME.log. <Last modified date of the file in given format>.log. And it will solve the problem.
July 15, 2009 at 3:40 am |
Hi, thanks for explaining and clearing my doubts with regards to the missing log files.
However, in the existing log files, although i uses ‘.’yyyy-MM-dd daily rotation at midnight. i realize that some log files have more than 1 day of logs in it… and i checked the server.log and there isn’t any renaming failure.
Any idea with regards to this issue?
July 15, 2009 at 10:20 am |
But I do not have any idea about your issue. Can u send the log4j settings file and a sample log file ??
Log4j backs up log files as you write logs to them. In log files update attempts log4j checks the current date with the last update date of the log file. If both dates are different, log4j backs up the current log file to a log file name of last update time. Due to this scenario, you may notice that logs of the last day do not back up until the first logging attempt of today. But this may not be the cause for your issue.
July 16, 2009 at 4:12 am |
Hi,
Thanks for the fast reply.
Below is the config for log4j
log4j.logger.myworld =DEBUG, INFO, dailyfileroll
log4j.appender.consoleTest=org.apache.log4j.ConsoleAppender
log4j.appender.consoleTest.layout=org.apache.log4j.PatternLayout
#log4j.appender.consoleTest.ConversionPattern=%d %5p [%t] (%F:%L) – %m%n
log4j.appender.consoleTest.Threshold = DEBUG
log4j.appender.dailyfileroll=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyfileroll.Threshold = DEBUG
log4j.appender.dailyfileroll.File=/opt/glassfish/domains/folder/myapplication.log
log4j.appender.dailyfileroll.DatePattern=.yyyy-MM-dd
log4j.appender.dailyfileroll.Append=true
log4j.appender.dailyfileroll.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyfileroll.layout.ConversionPattern=%-5p;%t;%F:%L;%m%n
E.g. My log file stated: myapplication.log.2009-07-10
But inside it has:
INFO ;main;LogManager.java:214;13-07-2009 …etc
I am suspecting because, i have 2 places writing to one log file.. one is normal app server and the other one is trigger by cron job as and when …
But they both share the same config ….
July 17, 2009 at 11:18 am |
can u send me a larger portion of your log file. Does
INFO ;main;LogManager.java:214;13-07-2009 …etc
log replicate in other log files too?
July 20, 2009 at 1:52 am |
this is a standard replicated by LogManager from persistence settings…