From 972507995c65e7529f4a03543acc5e04f53c7523 Mon Sep 17 00:00:00 2001 From: stefanp Date: Mon, 30 Sep 2002 09:39:43 +0000 Subject: [PATCH] - append only to logfiles that were lastmodified today - first name tried for archive doesn't have a counter anymore --- src/helma/util/Logger.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/helma/util/Logger.java b/src/helma/util/Logger.java index 3f2fcfa8..ccd51184 100644 --- a/src/helma/util/Logger.java +++ b/src/helma/util/Logger.java @@ -81,8 +81,13 @@ public final class Logger { logdir.mkdirs (); try { - // create a new log file, append to an existing file - writer = new PrintWriter (new FileWriter (logfile, true), false); + if (logfile.exists () && logfile.lastModified () < lastMidnight ()) { + // rotate if a log file exists and is NOT from today + rotateLogFile (); + } else { + // create a new log file, append to an existing file + writer = new PrintWriter (new FileWriter (logfile, true), false); + } } catch (IOException iox) { System.err.println ("Error creating log "+canonicalName+": "+iox); } @@ -205,10 +210,13 @@ public final class Logger { String today = aformat.format(new Date()); int ct=0; File archive = null; + // first append just the date + String archname = filename+"-"+today+".log.gz"; while (archive==null || archive.exists()) { - String archidx = ct>999 ? Integer.toString(ct) : nformat.format (++ct); - String archname = filename+"-"+today+"-"+ archidx +".log.gz"; archive = new File (logdir, archname); + // for the next try we append a counter + String archidx = ct>999 ? Integer.toString(ct) : nformat.format (++ct); + archname = filename+"-"+today+"-"+ archidx +".log.gz"; } if (logfile.renameTo (archive)) (new GZipper(archive)).start(); @@ -366,6 +374,9 @@ public final class Logger { return cal.getTimeInMillis (); } + public static long lastMidnight () { + return nextMidnight () - 86400000; + } /** * test main method