Bug 44932 - improve DailyRollingFileAppender handling of rotation errors
Summary: improve DailyRollingFileAppender handling of rotation errors
Status: NEW
Alias: None
Product: Log4j - Now in Jira
Classification: Unclassified
Component: Appender (show other bugs)
Version: 1.2
Hardware: PC Windows XP
: P2 critical
Target Milestone: ---
Assignee: log4j-dev
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-05-05 07:23 UTC by Howard
Modified: 2014-06-17 07:49 UTC (History)
5 users (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Howard 2008-05-05 07:23:27 UTC
Request to improve DailyRollingFileAppender handling of errors during log rotation.  If an error occur,

	P1.	Old logs [to archive] can be lost.  Can occur if scheduled-file=name [to archive to] cannot be deleted during rotation

	P2.	It can end up infinite looping if ErrorHandler itself uses DailyRollingFileAppender

To address above issues, I'm currently use customized version of DailyRollingFileAppender with the following changes:

	C1.	If cannot delete scheduled-file-name, then continue appending to existing file (i.e., don't rotate)

	C2.	Defer invocating ErrorHandler until very end of rollOver() method.

i.e.,  Change Log4j v1.2.15 from

	void rollOver() throws IOException {
		...

		if (target.exists()) {
			target.delete();
		}

		File file = new File(fileName);
		boolean result = file.renameTo(target);
		if(result) {
			LogLog.debug(fileName +" -> "+ scheduledFilename);
		} else {
			LogLog.error("Failed to rename ["+fileName+"] to ["+scheduledFilename+"].");
		}

		try {
			// This will also close the file. This is OK since multiple
			// close operations are safe.
			this.setFile(fileName, false, this.bufferedIO, this.bufferSize);
		}
		catch(IOException e) {
			errorHandler.error("setFile("+fileName+", false) call failed.");
		}

		...
	}

to

	void rollOver() throws IOException {
		...

		List<String> errMsgs = new LinkedList<String>();
		boolean result = true;

		if (target.exists()) {
			result = target.delete();
		}

		if (! result) {
			errMsgs.add("Failed to delete [" + scheduledFilename + "].  Log file will not be rolled over!");
		} else {
			File file = new File(fileName);
			result = file.renameTo(target);
			if(result) {
				LogLog.debug(fileName +" -> "+ scheduledFilename);
			} else {
				errMsgs.add("Failed to rename ["+fileName+"] to ["+scheduledFilename+"].  Log file will not be rolled over!");
			}
		}

		try {
			this.setFile(
					fileName,
					// Append if rollover [to schedule file] failed, else overwrite
					! result,
					this.bufferedIO,
					this.bufferSize);

			// Finally write out error messages now that Log4j is closer to being stable
			for (String msg : errMsgs) {
				errorHandler.error(msg);
			}
		}
		catch(IOException e) {
			errorHandler.error("setFile("+fileName+", false) call failed.");
		}

		...
	}
Comment 1 johan.guiheneuf 2008-06-02 01:25:33 UTC
Case P1 viewed on a computer with 4 procesors.
The same software is ran on the 4 processors. If one of the processors is running the software à 12PM, then all the log of the day is lost.
Comment 2 Andy Shen 2008-07-02 18:52:25 UTC
Can we apply the same logic to RollingFileAppender as well?

Cheers,
Andy
Comment 3 apache 2009-07-01 01:38:52 UTC
I lost a log file for a whole day due to issue P1, so I would say this is a critical Bug not only an enhancement.
Comment 4 bouliz 2010-11-12 15:57:21 UTC
Does log4g 1.2.16 solve this issue ?
Comment 5 gyadav 2014-06-17 07:47:09 UTC
Is this issue resolved in any of the log4j version? Please let me know.
Comment 6 gyadav 2014-06-17 07:49:56 UTC
And if there exists any workaround to resolve this issue, then also please let me know.

Thanks