Class Log
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'sLogView.ConsoleLogger— writes toSystem.out/System.errwith 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.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum number of recent messages retained for newly registered listeners. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddLogListener(ILogListener listener) Register a log listener.protected Objectclone()Singleton objects may not be cloned.voidSend a configuration message to all registered listeners.voidSend an error message to all registered listeners.voidSend aThrowable's full stack trace to all registered listeners atLog.Level.EXCEPTIONseverity.static LogReturns the singletonLoginstance.voidSend an informational message to all registered listeners.voidremoveLogListener(ILogListener listener) Unregister a log listener.voidSend a warning message to all registered listeners.
-
Field Details
-
HISTORY_CAPACITY
public static final int HISTORY_CAPACITYMaximum number of recent messages retained for newly registered listeners.- See Also:
-
-
Method Details
-
getInstance
Returns the singletonLoginstance.- Returns:
- the global logger; never
null
-
addLogListener
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 ifnull
-
removeLogListener
Unregister a log listener.If
listeneris not currently registered this is a no-op.- Parameters:
listener- the listener to remove; ignored ifnull
-
info
Send an informational message to all registered listeners.Use for normal application events: startup progress, successful operations, state transitions.
- Parameters:
message- the message; ignored ifnull
-
config
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 ifnull
-
warning
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 ifnull
-
error
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 ifnull
-
exception
Send aThrowable's full stack trace to all registered listeners atLog.Level.EXCEPTIONseverity.The throwable is converted to a string before dispatch; listeners receive a plain
Stringand never see the original object.- Parameters:
t- the throwable to log; ignored ifnull
-
clone
Singleton objects may not be cloned.- Overrides:
clonein classObject- Throws:
CloneNotSupportedException- always
-