Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

log4j RollingFileAppender not creating backup

Status
Not open for further replies.

nocomm

Programmer
May 23, 2003
16
AT
Hi all,

I have the problem that log4j does not create a backup file, when another java process (for example) also reads from this file. Here are my log4j properties:
------------------------------
log4j.category.allErrors=DEBUG, fileout_allErrors
log4j.appender.fileout_allErrors=org.apache.log4j.RollingFileAppender
log4j.appender.fileout_allErrors.File=C:/ican50/bhb_bhs/logs/allErrors.log
log4j.appender.fileout_allErrors.MaxFileSize=10MB
log4j.appender.fileout_allErrors.MaxBackupIndex=5
log4j.appender.fileout_allErrors.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout_allErrors.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss.SSS} - %5p [%t] - %m => %C{1}.%M%n
------------------------------

So, after 10 MB of data in the file allErrors.log, a backup should be created (allErrors.log.1) and all upcoming data should be logged into the new allErrors.log file.
This all works perfect, as long as no other process "monitors" the allErrors.log file.

My problem is, that I have a small java program that simulates the good all "tail" command from UNIX, so that I always see the latest information of the log file in my application. This process holds some kind of lock on the watched log-file (this is not explicetly done by me, but must be some implicit lock, I guess) and so log4j does not create the backup (allErrors.log.1) at all, but removes all data from the allErrors.log file and starts from 0 again. As a result the 10 MB previously logged (that have been in the allErrors.log) are lost!

Any ideas what I can do to solve this problem?!

Thanks for any help and regards
Bernhard Boehm
 
Is your java 'tail' program properly closing its file streams etc?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Hi Tim,

thanks for your answer - that is for sure the problem, because, as it is "tail" programm it keeps listening on the file as long as I say stop. So the file stream is surely open, when log4j tries to make the backup.
But, I hoped that there is a functionality in log4j, that this does not matter and the backup will be created anyway - because, what I do not understand is, that the original file is eliminated and an empty new one is started, and ONLY the backup of the old data is not done?!

Regards
Bernhard
 
Can't you update your 'tail' program to intermittently check the file, releasing the lock in-between?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Well, how is this possible? I would need to do this at the very right millisecond, in order not to let log4j delete it, in the meantime - or do you know a possibility?!

Thanks
Bernhard
 
Sorry, I see your point now.

Maybe you could use a more exotic solution; write your own appender by extending the RollingFileAppender. This could override the doAppend method and make new entries available to your application in whatever fashion you like. It would also need to call super.doAppend to ensure the entries also end up in the log files as per your settings.

This is entirely 'off the top of my head'; I apologise that I don't have the time to research / validate this approach.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top