Class Log

java.lang.Object
edu.cnu.mdi.log.Log

public class Log extends Object
Application-wide logger for the MDI framework.

Design

Log is a singleton that distributes log messages to zero or more registered ILogListener implementations. It retains a small bounded history and replays it to each newly registered listener, while all actual output remains the responsibility of listeners. The framework ships with three ready-made listeners:

  • LogPane — displays messages in a color-coded Swing text pane inside the application's LogView.
  • ConsoleLogger — writes to System.out / System.err with timestamps.
  • FileLogger — appends to a rotating log file.

Applications register whichever listeners suit their deployment:

 Log.getInstance().addLogListener(new ConsoleLogger());
 Log.getInstance().addLogListener(new FileLogger(Path.of("app.log")));
 

Log levels

Five levels are defined in order of increasing severity: Log.Level.INFO, Log.Level.CONFIG, Log.Level.WARNING, Log.Level.ERROR, and Log.Level.EXCEPTION. Each level dispatches to the corresponding method on ILogListener.

Thread safety

The singleton is initialized eagerly at class-load time, which is inherently thread-safe under the Java memory model. The listener list is managed by EventListenerList, whose iteration snapshot guarantees safe concurrent reads. Listener add/remove operations are synchronized on the instance. Message dispatch is unsynchronized and therefore lock-free on the hot path.

Listeners themselves are responsible for any thread-safety requirements they have. In particular, listeners that update Swing components must dispatch to the EDT.

  • Field Details

    • HISTORY_CAPACITY

      public static final int HISTORY_CAPACITY
      Maximum number of recent messages retained for newly registered listeners.
      See Also:
  • Method Details

    • getInstance

      public static Log getInstance()
      Returns the singleton Log instance.
      Returns:
      the global logger; never null
    • addLogListener

      public void addLogListener(ILogListener listener)
      Register a log listener.

      A new listener first receives the retained history in chronological order, followed by subsequent messages. Registering the same listener instance again is a no-op, preventing duplicate output and replay.

      Parameters:
      listener - the listener to add; ignored if null
    • removeLogListener

      public void removeLogListener(ILogListener listener)
      Unregister a log listener.

      If listener is not currently registered this is a no-op.

      Parameters:
      listener - the listener to remove; ignored if null
    • info

      public void info(String message)
      Send an informational message to all registered listeners.

      Use for normal application events: startup progress, successful operations, state transitions.

      Parameters:
      message - the message; ignored if null
    • config

      public void config(String message)
      Send a configuration message to all registered listeners.

      Use for application setup events: loaded resources, resolved file paths, applied settings.

      Parameters:
      message - the message; ignored if null
    • warning

      public void warning(String message)
      Send a warning message to all registered listeners.

      Use for recoverable problems or unexpected conditions that did not prevent the application from continuing.

      Parameters:
      message - the message; ignored if null
    • error

      public void error(String message)
      Send an error message to all registered listeners.

      Use for significant failures. The application may be able to continue, but the operation that triggered the error did not complete successfully.

      Parameters:
      message - the message; ignored if null
    • exception

      public void exception(Throwable t)
      Send a Throwable's full stack trace to all registered listeners at Log.Level.EXCEPTION severity.

      The throwable is converted to a string before dispatch; listeners receive a plain String and never see the original object.

      Parameters:
      t - the throwable to log; ignored if null
    • clone

      protected Object clone() throws CloneNotSupportedException
      Singleton objects may not be cloned.
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException - always