Package org.ka2ddo.yaac.core
Class FileLogger<T>
java.lang.Object
org.ka2ddo.util.ReschedulableTimerTask
org.ka2ddo.yaac.core.FileLogger<T>
- All Implemented Interfaces:
Comparable<ReschedulableTimerTask>
,Runnable
,ShutdownHandler
- Direct Known Subclasses:
AX25Logger
,StringLogger
This class implements a generic time-batched logging facility for an arbitrary
record type T in a date-rolled sequential logging file.
- Author:
- Andrew Pavlin, KA2DDO
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Define a handler to be called when the logger rolls over to a new file. -
Constructor Summary
ModifierConstructorDescriptionprotected
FileLogger
(String prefix, String datePattern, String suffix, int flushEveryNSeconds) Create a FileLogger using the specified file prefix, date pattern, suffix, and flush interval, storing the log files in YAAC's default log directory.protected
FileLogger
(String prefix, String datePattern, String suffix, int flushEveryNSeconds, long rolloverInterval) Create a FileLogger using the specified file prefix, date pattern, suffix, and flush interval, storing the log files in YAAC's default log directory.protected
FileLogger
(String prefix, String datePattern, String suffix, int flushEveryNSeconds, String logDirPath) Create a FileLogger using the specified file prefix, date pattern, suffix, and flush interval, storing the log files in the explicitly specified log directory. -
Method Summary
Modifier and TypeMethodDescriptionabstract void
dumpRecordQueueToFile
(File logFileFile, ArrayList<T> recordQueue) Subclasses should implement this method to open the specified sequential file for append, then write each of the records in the recordQueue in order to the file, then close the file.void
flush()
Dump the current contents of the backlog queue to the current log file.static File
Get the current default directory used for log files.void
Append one record to the log file, using asynchronous background writing.void
Append one record to the log file, using asynchronous background writing.void
Append one record to the log file, using asynchronous background writing.final void
run()
DO NOT CALL.void
setDatePattern
(String datePattern) Set the pattern for formatting the date stamp in the log file name.static void
setDefaultLogDir
(String defaultLogDirPath) Specify the default directory where all log files will be kept.void
setFilePrefix
(String prefix) Change the constant prefix that should be used before the date part of the log file name.void
setFileSuffix
(String suffix) Change the constant suffix that should be used after the date part of the log file name.void
setFlushInterval
(int flushEveryNSeconds) Set how frequently the logger should dump its backlog queue to disk and close the file for checkpointing.void
setLogDirectory
(String logDirPath) Set the directory in which log files should be created.void
setRolloverHandler
(FileLogger.RolloverHandler<T> rolloverHandler) Set (or clear) the handler to be informed whenever the FileLogger advances to a new periodic log file.void
setRolloverInterval
(long rolloverInterval) Set the interval between closing of one log file and opening of the next.void
setTimeZone
(TimeZone tz) Set the time zone for use in calculating file rollovers.void
shutdown()
Do whatever cleanup this object needs for program shutdown.
-
Constructor Details
-
FileLogger
Create a FileLogger using the specified file prefix, date pattern, suffix, and flush interval, storing the log files in YAAC's default log directory.- Parameters:
prefix
- String file name prefix (before the date)datePattern
- format string for file name embedded date, using SimpleDateFormat directivessuffix
- String file name suffix (after the date)flushEveryNSeconds
- the interval between log flushes in seconds- See Also:
-
FileLogger
protected FileLogger(String prefix, String datePattern, String suffix, int flushEveryNSeconds, long rolloverInterval) Create a FileLogger using the specified file prefix, date pattern, suffix, and flush interval, storing the log files in YAAC's default log directory.- Parameters:
prefix
- String file name prefix (before the date)datePattern
- format string for file name embedded date, using SimpleDateFormat directivessuffix
- String file name suffix (after the date)flushEveryNSeconds
- the interval between log flushes in secondsrolloverInterval
- the interval in milliseconds between log file changes- See Also:
-
FileLogger
protected FileLogger(String prefix, String datePattern, String suffix, int flushEveryNSeconds, String logDirPath) Create a FileLogger using the specified file prefix, date pattern, suffix, and flush interval, storing the log files in the explicitly specified log directory.- Parameters:
prefix
- String file name prefix (before the date)datePattern
- format string for file name embedded date, using SimpleDateFormat directivessuffix
- String file name suffix (after the date)flushEveryNSeconds
- the interval between log flushes in secondslogDirPath
- String path name of directory to contain the log files
-
-
Method Details
-
setLogDirectory
Set the directory in which log files should be created. If the directory does not exist, this method will attempt to create it.- Parameters:
logDirPath
- String pathname of log-containing directory
-
setDefaultLogDir
Specify the default directory where all log files will be kept.- Parameters:
defaultLogDirPath
- String absolute path to a directory
-
getDefaultLogDir
Get the current default directory used for log files. Note that individual loggers can override this.- Returns:
- File of default log directory (null if not set yet)
-
setFlushInterval
public void setFlushInterval(int flushEveryNSeconds) Set how frequently the logger should dump its backlog queue to disk and close the file for checkpointing.- Parameters:
flushEveryNSeconds
- the interval in seconds between file closes
-
setTimeZone
Set the time zone for use in calculating file rollovers. The rollover interval should be whole divisions of a day (up to a full day), and are aligned with the time zone's midnight.- Parameters:
tz
- TimeZone to use for calculating rollovers
-
setRolloverInterval
public void setRolloverInterval(long rolloverInterval) Set the interval between closing of one log file and opening of the next. Note that these intervals are measured relative to the FileLogger's time zone's midnight, so it is recommended that the chosen value be an integer fraction of 86400000 (the number of milliseconds in a day).- Parameters:
rolloverInterval
- time span for each log file instance in milliseconds
-
setDatePattern
Set the pattern for formatting the date stamp in the log file name. This uses Java SimpleDateFormat formatting, and it is suggested that the user not specify a format with whitespace in it, to prevent command-line issues in dealing with the created files. The user is responsible for specifying a format whose resolution is commensurate with the rollover interval. For example, specifying a rolloverInterval of 1 hour but having a datePattern only showing the date without the hour of day would cause all 24 hours of log files to have the same name.- Parameters:
datePattern
- String pattern for generating the date stamp in the file name- See Also:
-
setFilePrefix
Change the constant prefix that should be used before the date part of the log file name.- Parameters:
prefix
- String name prefix
-
setFileSuffix
Change the constant suffix that should be used after the date part of the log file name.- Parameters:
suffix
- String name suffix
-
log
Append one record to the log file, using asynchronous background writing.- Parameters:
record
- data record to log of the specified generic type
-
log
Append one record to the log file, using asynchronous background writing.- Parameters:
record
- data record to log of the specified generic typedate
- Date when record was created, received, etc.
-
log
Append one record to the log file, using asynchronous background writing.- Parameters:
record
- data record to log of the specified generic typedate
- long timestamp in milliseconds since midnight Jan 19 1970 UTC when record was created, received, etc.
-
flush
public void flush()Dump the current contents of the backlog queue to the current log file. -
setRolloverHandler
Set (or clear) the handler to be informed whenever the FileLogger advances to a new periodic log file.- Parameters:
rolloverHandler
- RolloverHandler instance to be informed of rollovers, or null to no longer be informed
-
dumpRecordQueueToFile
public abstract void dumpRecordQueueToFile(File logFileFile, ArrayList<T> recordQueue) throws IOException Subclasses should implement this method to open the specified sequential file for append, then write each of the records in the recordQueue in order to the file, then close the file.- Parameters:
logFileFile
- File identifying the log file to open and writerecordQueue
- the ArrayList of ordered records to write- Throws:
IOException
- if record saving fails for any reason
-
run
public final void run()DO NOT CALL. Task for executing periodic flushes of log file. -
shutdown
public void shutdown()Do whatever cleanup this object needs for program shutdown.- Specified by:
shutdown
in interfaceShutdownHandler
-