logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Horstmann, Gero" <Gero.Horstm...@AXA.de>
Subject Forced "Rolling" on midnight with DatedFileAppender
Date Thu, 21 Apr 2005 11:53:56 GMT
Hi!

We are using the DatedFileAppender for writing webcontrolling information, these informations
have to be moved from the windows application servers once a day to other systems.
Up to now we did shut down the Tomcat for maintenance and moved the files during this downtime,
but now we want to minimize the downtime, so we won't shut down the servers.

The problem that now occurs was already discussed on this list in 2002, the DatedFileAppender
(and all other Appenders that do "roll" in one way or other) will only roll when an event
is logged after the given time (or size) for that it is configured.
Until this happens, the file will be kept locked.
This leads to the problem, that if no event is logged for e.g. 2 days you would find a file
on the system that has a date in it that's 2 days old but still active.
This problem was already discussed on this list some time ago with the solution that people
just should force the logging of an event. This is not possible for our purpose.

To do a quick fix to our problem I changed the DatedFileAppender.setFile(DatedFile, boolean)
method to start a thread that triggers the rolling at midnight:
====
	public void setFile(DatedFile datedFile, boolean append) throws IOException {
		Calendar now = Calendar.getInstance();
		String datedFilename = datedFile.getDatedFilename(now);
		setFile(datedFilename, append, false, 0);
		nextMidnight = datedFile.nextFilenameChange(now);
		
>		Thread kb = new Thread(new DatedFileRoller(nextMidnight, this));
>		kb.start();
		
		LogLog.debug("Opened " + datedFilename);
		LogLog.debug("Next rollover: " + new Date(nextMidnight));
	}
===
and added a method to access the value of nextMidnight and the datedFile from outside:
===
	/**
	 * @return Returns the datedFile.
	 */
	protected DatedFile getDatedFile() {
		return datedFile;
	}
	
	/**
	 * @return Returns the nextMidnight.
	 */
	protected long getNextMidnight() {
		return nextMidnight;
	}
===

The new class DatedFileRoller just waits till midnight and calls setFile of the DatedFileAppender:

=================
	public DatedFileRoller(long millies, DatedFileAppender appender) {
		this.millies = millies;
		this.dfa = appender;
	}

	public void run() {
		Thread t = Thread.currentThread();
		t.setPriority(Thread.MIN_PRIORITY);
		try {
			// Warten bis Mitternacht
			Thread.sleep(Math.abs(millies - System.currentTimeMillis()));
		} catch (InterruptedException e) {
			logger.error(e);
		}
		if (dfa != null) {
			if (dfa.getNextMidnight() == millies) {
				// Still same midnight? perhaps it was already rolled by event?
				try {
					dfa.setFile(dfa.getDatedFile(), dfa.fileAppend);
				} catch (IOException e1) {
					logger.error(e1);
				}
			}
		}
=================

Someone else here suggested the Usage of a Timer instead, to prevent locks:

=================
		final Timer timer = new Timer();
		timer.schedule(new TimerTask(){
		   public void run(){
			
			//roll here

			timer.cancel();
		 }}, Math.abs(millies - System.currentTimeMillis()));
=================

Did anyone else find a solutin for this problem? 
I'd prefer a solution that is already tested and published (and maybe in the cvs at some time
in the future) over someting that I wrote/modified myself.

Thanks, 
G.Horstmann

---
> AXA Service AG
> GERO HORSTMANN
> SOFTWAREENTWICKLER ISDM-BCC
> Colonia-Allee 10-20; D-51067 Köln
> Telefon:+49 (0)221 / 148 - 21 7 84
> Telefax:+49 (0)221 / 148 - 44 21 7 84
> E-Mail: Gero.Horstmann@axa.de



------------------------------------------------------------------------------
Aus Rechts- und Sicherheitsgruenden ist die in dieser E-Mail gegebene Information nicht rechtsverbindlich.
Eine rechtsverbindliche Bestaetigung reichen wir Ihnen gerne auf Anforderung in schriftlicher
Form nach. Beachten Sie bitte, dass jede Form der unautorisierten Nutzung, Veroeffentlichung,
Vervielfaeltigung oder Weitergabe des Inhalts dieser E-Mail nicht gestattet ist. Diese Nachricht
 ist ausschliesslich fuer den bezeichneten Adressaten oder dessen Vertreter bestimmt. Sollten
Sie nicht der vorgesehene Adressat dieser E-Mail oder dessen Vertreter sein, so bitten wir
Sie, sich mit dem Absender der E-Mail in Verbindung zu setzen.
----------------------------
For legal and security reasons the information provided in this e-mail is not legally binding.
Upon request we would be pleased to provide you with a legally binding confirmation in written
form. Any form of unauthorised use, publication, reproduction, copying or disclosure of the
content of this e-mail is not permitted. This message is exclusively for the person addressed
or their representative. If you are not the intended recipient of this message and its contents,
please notify the sender immediately.

==============================================================================


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message