Class ZLog

java.lang.Object
org.spiderwiz.zutils.ZLog

public class ZLog
extends Object
Provides logging mechanism.

The class provides a framework for a logging mechanism that includes the following features:

  • Management an hierarchy of log folders and files under a root log folder.
  • Automatic creation of log folders for every calendar day and log files for every hour of the day.
  • Optional appending of timestamps and headers to each log line.
  • Various log methods with and without formatting.

For details about the framework logging system see Spiderwiz Logging System.

  • Constructor Summary

    Constructors 
    Constructor Description
    ZLog​(String rootFolder, ZConfig config)
    Constructs an appending logger.
    ZLog​(String rootFolder, ZConfig config, boolean append)
    Constructs an appending or overwriting logger.
  • Method Summary

    Modifier and Type Method Description
    void cleanup()
    Flushes and closes the current log file.
    void commit​(Date time, String header, String line, boolean keepSame)
    Writes a line to the current log file.
    void flush()
    Flushes the current log file.
    String getRootPath()
    Returns the root pathname assigned to the logger.
    void init​(String label)
    Initializes the logger.
    ZLog log​(String line, boolean keepSame)
    Logs one line in a log file with the current time as a timestamp.
    ZLog logEvent​(String fmt, Object... args)
    Formats a line using the given format string and arguments, writes it to a log file with the current time as a timestamp, flushes the file and also writes the line to the console.
    ZLog logException​(Throwable ex)
    Calls logEvent() to log an event with the given exception as text, then follows it in the file and on the console by the stack trace of the exception.
    ZLog logf​(String fmt, Object... args)
    Formats a line using the given format string and arguments and writes it to a log file with the current time as a timestamp.
    ZLog logNow​(String fmt, Object... args)
    Formats a line using the given format string and arguments, writes it to a log file with the current time as a timestamp and flushes the file.
    void setRootPath​(String rootPath)
    Sets a new root pathname to the logger.

    Methods inherited from class java.lang.Object

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

    • ZLog

      public ZLog​(String rootFolder, ZConfig config)
      Constructs an appending logger.

      The log folder managed by the logger is determined by the given root folder name and a property taken from the given configuration file. The label of the property that defines the log sub-folder name is provided when init() is called.

      If the log folder already contains hourly files for an hour that is logged by this logger then new logs are appended to them.

      Parameters:
      rootFolder - the root folder name.
      config - the configuration file containing the property that determines the log sub-folder name.
    • ZLog

      public ZLog​(String rootFolder, ZConfig config, boolean append)
      Constructs an appending or overwriting logger.

      The log folder managed by the logger is determined by the given root folder name and a property taken from the given configuration object. The label of the property that defines the log sub-folder name is provided when init() is called.

      The append parameter determines whether this is an appending or overwriting logger. If the log folder already contains hourly files for an hour that is logged by this logger, then an appending logger appends to them while an overwriting logger overwrites them.

      Parameters:
      rootFolder - the root folder name.
      config - the configuration object containing the property that determines the log sub-folder name.
      append - true for an appending logger, false for an overwriting logger.
  • Method Details

    • init

      public void init​(String label)
      Initializes the logger.

      The label parameter identifies the property in the configuration file attached by the class constructor that specifies the log sub-folder name.

      Parameters:
      label - identifies the property in the configuration file attached to this logger that specifies the log sub-folder name.
    • commit

      public void commit​(Date time, String header, String line, boolean keepSame)
      Writes a line to the current log file.

      Writes the line string to the current log file. If time is not null then the method:

      1. Precedes the line written to the log file with a timestamp in the format hh:mm:ss:SSS whose value is specified by the parameter.
      2. Writes the line to a file under the current log sub-folder named amhh or pmhh, reflecting the given time.
      3. If keepSame parameter is false, then if time spans into a new hour since the last commit creates a new file (and also a new folder if the date has changed), or appends to an existing file if a file with that names exists and this is an appending logger.
      Parameters:
      time - if not null the line is preceded by this timestamp and written to a file whose name is determined by this time.
      header - if not null the timestamp (if given) is followed by this header.
      line - the line to write to the file.
      keepSame - if true writes to the same file as the previous commit even though the hour has changed since then.
    • log

      public ZLog log​(String line, boolean keepSame)
      Logs one line in a log file with the current time as a timestamp.
      Parameters:
      line - the line to log.
      keepSame - if true writes to the same file as the previous commit even though the hour has changed since then.
      Returns:
      this object
      See Also:
      commit()
    • logf

      public ZLog logf​(String fmt, Object... args)
      Formats a line using the given format string and arguments and writes it to a log file with the current time as a timestamp.
      Parameters:
      fmt - the given format string.
      args - the given arguments.
      Returns:
      this object
      See Also:
      log()
    • logNow

      public ZLog logNow​(String fmt, Object... args)
      Formats a line using the given format string and arguments, writes it to a log file with the current time as a timestamp and flushes the file.
      Parameters:
      fmt - the given format string.
      args - the given arguments.
      Returns:
      this object
      See Also:
      logf()
    • logEvent

      public ZLog logEvent​(String fmt, Object... args)
      Formats a line using the given format string and arguments, writes it to a log file with the current time as a timestamp, flushes the file and also writes the line to the console.
      Parameters:
      fmt - the given format string.
      args - the given arguments.
      Returns:
      this object
      See Also:
      logNow()
    • logException

      public ZLog logException​(Throwable ex)
      Calls logEvent() to log an event with the given exception as text, then follows it in the file and on the console by the stack trace of the exception.
      Parameters:
      ex - the given exception.
      Returns:
      this object
      See Also:
      logEvent()
    • flush

      public void flush()
      Flushes the current log file.
    • cleanup

      public void cleanup()
      Flushes and closes the current log file.
    • getRootPath

      public String getRootPath()
      Returns the root pathname assigned to the logger.

      A root path is assigned by the class consructor or by setRootPath().

      Returns:
      the root pathname assigned to the logger.
      See Also:
      setRootPath()
    • setRootPath

      public void setRootPath​(String rootPath)
      Sets a new root pathname to the logger.
      Parameters:
      rootPath - root pathname to set.
      See Also:
      getRootPath()