Class ConsoleLogger

java.lang.Object
edu.cnu.mdi.log.ConsoleLogger
All Implemented Interfaces:
ILogListener, EventListener

public class ConsoleLogger extends Object implements ILogListener
An ILogListener that writes log messages to the standard console streams.

Output streams

Log.Level.INFO and Log.Level.CONFIG messages are written to System.out. Log.Level.WARNING, Log.Level.ERROR, and Log.Level.EXCEPTION messages are written to System.err so that they appear in red in most terminal emulators and are captured separately by process supervisors and IDEs that distinguish the two streams.

Format

Each line is prefixed with the current wall-clock time (HH:mm:ss) and a fixed-width level tag:

 09:14:02 [INFO   ] Application started.
 09:14:02 [CONFIG ] Loaded 195 countries from GeoJSON resource.
 09:14:05 [WARNING] Configuration file not found; using defaults.
 09:14:07 [ERROR  ] Failed to open data file: foo.dat
 09:14:07 [EXCEPT ] java.io.FileNotFoundException: foo.dat ...
 

Multi-line messages (such as exception stack traces) are indented on continuation lines so the level tag visually anchors the start of each entry.

Registration

 Log.getInstance().addLogListener(new ConsoleLogger());
 

Selective levels

To suppress a level, subclass ConsoleLogger and override the corresponding method as a no-op, or construct a targeted anonymous ILogListener directly.

Thread safety

Each format(Log.Level, String) call produces a single PrintStream.println(String) which is atomic on all standard JVM implementations. Interleaving between threads is therefore limited to whole messages rather than partial lines.

  • Constructor Details

    • ConsoleLogger

      public ConsoleLogger()
  • Method Details

    • info

      public void info(String message)
      Writes an Log.Level.INFO message to System.out.
      Specified by:
      info in interface ILogListener
      Parameters:
      message - the message; never null
    • config

      public void config(String message)
      Writes a Log.Level.CONFIG message to System.out.
      Specified by:
      config in interface ILogListener
      Parameters:
      message - the message; never null
    • warning

      public void warning(String message)
      Writes a Log.Level.WARNING message to System.err.
      Specified by:
      warning in interface ILogListener
      Parameters:
      message - the message; never null
    • error

      public void error(String message)
      Writes a Log.Level.ERROR message to System.err.
      Specified by:
      error in interface ILogListener
      Parameters:
      message - the message; never null
    • exception

      public void exception(String message)
      Writes a Log.Level.EXCEPTION message (stack trace) to System.err.
      Specified by:
      exception in interface ILogListener
      Parameters:
      message - the formatted stack trace; never null
    • format

      protected String format(Log.Level level, String message)
      Format a log message as a single string ready for printing.

      The first line receives the timestamp and level tag. Any subsequent lines (common in stack traces) are indented to align with the message text of the first line, keeping the output readable without repeating the prefix on every line.

      Parameters:
      level - the log level
      message - the raw message text
      Returns:
      the formatted, ready-to-print string