Allow longer rollover periods than daily.
Also allow rollover to be disabled.
This commit is contained in:
@@ -36,17 +36,30 @@ public class AuditLogger
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an audit logger that logs to the specified file.
|
* Creates an audit logger that logs to the specified file. The log will be rolled over daily.
|
||||||
*/
|
*/
|
||||||
public AuditLogger (File fullpath)
|
public AuditLogger (File fullpath)
|
||||||
|
{
|
||||||
|
this(fullpath, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an audit logger that logs to the specified file. The log will be rolled over every
|
||||||
|
* {@code rolloverDays} days. If {@code rolloverDays} is zero (or less than zero) the log will
|
||||||
|
* never be rolled over.
|
||||||
|
*/
|
||||||
|
public AuditLogger (File fullpath, int rolloverDays)
|
||||||
{
|
{
|
||||||
_logPath = fullpath;
|
_logPath = fullpath;
|
||||||
|
_rolloverDays = rolloverDays;
|
||||||
openLog(true);
|
openLog(true);
|
||||||
|
|
||||||
// update the day format
|
if (rolloverDays > 0) {
|
||||||
|
// update the day stamp
|
||||||
_dayStamp = _dayFormat.format(new Date());
|
_dayStamp = _dayFormat.format(new Date());
|
||||||
scheduleNextRolloverCheck();
|
scheduleNextRolloverCheck();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a log entry out of the given message and args using
|
* Builds a log entry out of the given message and args using
|
||||||
@@ -129,7 +142,7 @@ public class AuditLogger
|
|||||||
String newDayStamp = _dayFormat.format(new Date());
|
String newDayStamp = _dayFormat.format(new Date());
|
||||||
|
|
||||||
// hey! we need to roll it over!
|
// hey! we need to roll it over!
|
||||||
if (!newDayStamp.equals(_dayStamp)) {
|
if (!newDayStamp.equals(_dayStamp) && daysSince(_dayStamp) >= _rolloverDays) {
|
||||||
log("log_closed");
|
log("log_closed");
|
||||||
_logWriter.close();
|
_logWriter.close();
|
||||||
_logWriter = null;
|
_logWriter = null;
|
||||||
@@ -150,6 +163,20 @@ public class AuditLogger
|
|||||||
scheduleNextRolloverCheck();
|
scheduleNextRolloverCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of days elapsed since the given day stamp.
|
||||||
|
*/
|
||||||
|
protected int daysSince (String dayStamp)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
long stampTime = _dayFormat.parse(dayStamp).getTime();
|
||||||
|
long nowTime = new Date().getTime();
|
||||||
|
return (int)((nowTime - stampTime) / (24L * 60L * 60L * 1000L));
|
||||||
|
} catch (java.text.ParseException pe) {
|
||||||
|
return Integer.MAX_VALUE; // trigger rollover on parse failure
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Schedule the next check to see if we should roll the logs over.
|
* Schedule the next check to see if we should roll the logs over.
|
||||||
*/
|
*/
|
||||||
@@ -175,6 +202,9 @@ public class AuditLogger
|
|||||||
/** The path to our log file. */
|
/** The path to our log file. */
|
||||||
protected File _logPath;
|
protected File _logPath;
|
||||||
|
|
||||||
|
/** The number of days between log rollovers. */
|
||||||
|
protected int _rolloverDays;
|
||||||
|
|
||||||
/** We actually write to this feller here. */
|
/** We actually write to this feller here. */
|
||||||
protected PrintWriter _logWriter;
|
protected PrintWriter _logWriter;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user