Class CalendarFileHandler

  • All Implemented Interfaces:
    TimerTaskListener

    public final class CalendarFileHandler
    extends java.util.logging.StreamHandler
    implements TimerTaskListener
    Logs messages to a user-specified file, rolling over to a new file at midnight. Log files are kept for only so many days before CalendarFileHandler deletes them. This retention limit is configurable but defaults to 10 days.

    The CalendarFileHandler uses three parameters to generate the complete file name:

     <base name>.<date pattern>.<extension>
     
    1. Base Name: Store the log files here using this base name.

      Example: /var/log/app/app

    2. Date Pattern: Use this pattern to format the date portion of the file name.

      The data pattern is passed to a SimpleDateFormat(String). See SimpleDateFormat for a detailed explanation of valid date formats.

    3. Extension: This is first part of the log file's name.

      Example: log

    Given the base name /var/log/eBus/eBus, a date pattern "ddMMyyyy" and extension log, the July 15, 2001 log file name is /var/log/eBus/eBus.15072001.log

    Configuration: CalendarFileHandler default configuration uses the following LogManager properties. If the named properties are either not defined or have invalid values, then the default settings are used.

    • net.sf.eBus.util.logging.CalendarFileHandler.basename (defaults to "./Logger")
    • net.sf.eBus.util.logging.CalendarFileHandler.pattern (defaults to "yyyyMMdd")
    • net.sf.eBus.util.logging.CalendarFileHandler.extension (defaults to "log")
    • net.sf.eBus.util.logging.CalendarFileHandler.days_kept (defaults to 10 days)
    • net.sf.eBus.util.logging.CalendarFileHandler.formatter (defaults to "net.sf.eBus.util.logging.PatternFormatter")
    • net.sf.eBus.util.logging.CalendarFileHandler.level (defaults to system default)
    Author:
    Charles Rapp
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_BASENAME
      The log file's default base name is "Logger".
      static java.lang.String DEFAULT_DATE_FORMAT
      The log file's default data format is "yyyyMMdd".
      static int DEFAULT_DAYS_KEPT
      Log files are kept for 10 days by default.
      static java.lang.String DEFAULT_DIRECTORY
      Log files are placed in the application's current working directory (".") by default.
      static java.lang.String DEFAULT_EXTENSION
      The default file extension is "log".
      static java.util.logging.Level DEFAULT_LEVEL
      The log file handler default level is Level.INFO.
      static int MAX_DAYS_KEPT
      The maximum number of days a log file is kept is 96.
      static int MIN_DAYS_KEPT
      The minimum number of days a log file is kept is 0 which means that the file is deleted as soon as the day ends.
    • Constructor Summary

      Constructors 
      Constructor Description
      CalendarFileHandler()
      Creates a new CalendarFileHandler and configures it according to LogManager configuration properties.
      CalendarFileHandler​(java.lang.String baseName, java.lang.String datePattern, java.lang.String extension, int daysKept)
      Creates a new CalendarFileHandler instance for the specified base name, date format pattern, file name extension and how long to keep the files around.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void handleTimeout​(TimerEvent task)
      Time to roll over to the next log file.
      void publish​(java.util.logging.LogRecord logRecord)
      Flushes the output stream after StreamHandler publishes the log record.
      • Methods inherited from class java.util.logging.StreamHandler

        close, flush, isLoggable, setEncoding, setOutputStream
      • Methods inherited from class java.util.logging.Handler

        getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError, setErrorManager, setFilter, setFormatter, setLevel
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_DIRECTORY

        public static final java.lang.String DEFAULT_DIRECTORY
        Log files are placed in the application's current working directory (".") by default.
        See Also:
        Constant Field Values
      • DEFAULT_BASENAME

        public static final java.lang.String DEFAULT_BASENAME
        The log file's default base name is "Logger".
        See Also:
        Constant Field Values
      • DEFAULT_DATE_FORMAT

        public static final java.lang.String DEFAULT_DATE_FORMAT
        The log file's default data format is "yyyyMMdd". For July 4, 1776 the formatted string is "17760704".
        See Also:
        Constant Field Values
      • DEFAULT_LEVEL

        public static final java.util.logging.Level DEFAULT_LEVEL
        The log file handler default level is Level.INFO.
      • DEFAULT_EXTENSION

        public static final java.lang.String DEFAULT_EXTENSION
        The default file extension is "log".
        See Also:
        Constant Field Values
      • MIN_DAYS_KEPT

        public static final int MIN_DAYS_KEPT
        The minimum number of days a log file is kept is 0 which means that the file is deleted as soon as the day ends.
        See Also:
        Constant Field Values
      • MAX_DAYS_KEPT

        public static final int MAX_DAYS_KEPT
        The maximum number of days a log file is kept is 96. That's three months. That is plenty of time to archive the file if necessary.
        See Also:
        Constant Field Values
      • DEFAULT_DAYS_KEPT

        public static final int DEFAULT_DAYS_KEPT
        Log files are kept for 10 days by default.
        See Also:
        Constant Field Values
    • Constructor Detail

      • CalendarFileHandler

        public CalendarFileHandler()
                            throws java.io.IOException,
                                   java.lang.ClassNotFoundException,
                                   java.lang.NoSuchMethodException,
                                   java.lang.InstantiationException,
                                   java.lang.IllegalAccessException,
                                   java.lang.reflect.InvocationTargetException
        Creates a new CalendarFileHandler and configures it according to LogManager configuration properties.
        Throws:
        java.lang.IllegalArgumentException - if LogManager configuration properties contains invalid or incorrect settings.
        java.io.IOException - if defined log directory is invalid.
        java.lang.ClassNotFoundException - if defined formatter is not a known Java class.
        java.lang.NoSuchMethodException - if defined formatter does not have a default constructor defined.
        java.lang.InstantiationException - if defined formatter instantiation failed.
        java.lang.IllegalAccessException - if defined formatter default constructor is inaccessible.
        java.lang.reflect.InvocationTargetException - if formatter instantiation failed.
      • CalendarFileHandler

        public CalendarFileHandler​(java.lang.String baseName,
                                   java.lang.String datePattern,
                                   java.lang.String extension,
                                   int daysKept)
                            throws java.io.IOException,
                                   java.lang.ClassNotFoundException,
                                   java.lang.NoSuchMethodException,
                                   java.lang.InstantiationException,
                                   java.lang.IllegalAccessException,
                                   java.lang.reflect.InvocationTargetException
        Creates a new CalendarFileHandler instance for the specified base name, date format pattern, file name extension and how long to keep the files around.
        Parameters:
        baseName - where to put the log files.
        datePattern - date format
        extension - file name extension
        daysKept - how long the log files are kept around (in days).
        Throws:
        java.lang.IllegalArgumentException - if:
        • if baseName, datePattern or extension is null.
        • baseName, datePattern or extension is an empty string.
        • daysKept is < MIN_DAYS_KEPT or > MAX_DAYS_KEPT.
        • datePattern is an invalid date format pattern as per java.text.SimpleDateFormat.
        • baseName is in an unknown directory or directory cannot be accessed.
        java.lang.IllegalArgumentException - if any of the given parameters is set to an invalid value.
        java.io.IOException - if defined log directory is invalid.
        java.lang.ClassNotFoundException - if defined formatter is not a known Java class.
        java.lang.NoSuchMethodException - if defined formatter does not have a default constructor defined.
        java.lang.InstantiationException - if defined formatter instantiation failed.
        java.lang.IllegalAccessException - if defined formatter default constructor is inaccessible.
        java.lang.reflect.InvocationTargetException - if formatter instantiation failed.
    • Method Detail

      • handleTimeout

        public void handleTimeout​(TimerEvent task)
        Time to roll over to the next log file.
        Specified by:
        handleTimeout in interface TimerTaskListener
        Parameters:
        task - the roll file timer task.
      • publish

        public void publish​(java.util.logging.LogRecord logRecord)
        Flushes the output stream after StreamHandler publishes the log record. StreamHandler does not do this which means records are not seen in the log file as they are published.
        Overrides:
        publish in class java.util.logging.StreamHandler
        Parameters:
        logRecord - Publish this log record to the log file.