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

public abstract class FileLogger<T> extends ReschedulableTimerTask implements ShutdownHandler
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

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Define a handler to be called when the logger rolls over to a new file.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    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 Type
    Method
    Description
    abstract 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
    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
    log(T record)
    Append one record to the log file, using asynchronous background writing.
    void
    log(T record, long date)
    Append one record to the log file, using asynchronous background writing.
    void
    log(T record, Date date)
    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
    Change the constant prefix that should be used before the date part of the log file name.
    void
    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
    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
    Set the time zone for use in calculating file rollovers.
    void
    Do whatever cleanup this object needs for program shutdown.

    Methods inherited from class org.ka2ddo.util.ReschedulableTimerTask

    cancel, compareTo, equals, hashCode, isActive, resched, resched, resched, resched

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileLogger

      protected 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.
      Parameters:
      prefix - String file name prefix (before the date)
      datePattern - format string for file name embedded date, using SimpleDateFormat directives
      suffix - 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 directives
      suffix - String file name suffix (after the date)
      flushEveryNSeconds - the interval between log flushes in seconds
      rolloverInterval - 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 directives
      suffix - String file name suffix (after the date)
      flushEveryNSeconds - the interval between log flushes in seconds
      logDirPath - String path name of directory to contain the log files
  • Method Details

    • setLogDirectory

      public void setLogDirectory(String logDirPath)
      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

      public static void setDefaultLogDir(String defaultLogDirPath)
      Specify the default directory where all log files will be kept.
      Parameters:
      defaultLogDirPath - String absolute path to a directory
    • getDefaultLogDir

      public static File 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

      public void setTimeZone(TimeZone tz)
      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

      public void setDatePattern(String datePattern)
      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

      public void setFilePrefix(String prefix)
      Change the constant prefix that should be used before the date part of the log file name.
      Parameters:
      prefix - String name prefix
    • setFileSuffix

      public void setFileSuffix(String suffix)
      Change the constant suffix that should be used after the date part of the log file name.
      Parameters:
      suffix - String name suffix
    • log

      public void log(T record)
      Append one record to the log file, using asynchronous background writing.
      Parameters:
      record - data record to log of the specified generic type
    • log

      public void log(T record, Date date)
      Append one record to the log file, using asynchronous background writing.
      Parameters:
      record - data record to log of the specified generic type
      date - Date when record was created, received, etc.
    • log

      public void log(T record, long date)
      Append one record to the log file, using asynchronous background writing.
      Parameters:
      record - data record to log of the specified generic type
      date - 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

      public void setRolloverHandler(FileLogger.RolloverHandler<T> rolloverHandler)
      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 write
      recordQueue - 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.
      Specified by:
      run in interface Runnable
    • shutdown

      public void shutdown()
      Do whatever cleanup this object needs for program shutdown.
      Specified by:
      shutdown in interface ShutdownHandler