Class ConsoleLogger
- All Implemented Interfaces:
ILogListener,EventListener
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidWrites aLog.Level.CONFIGmessage toSystem.out.voidWrites aLog.Level.ERRORmessage toSystem.err.voidWrites aLog.Level.EXCEPTIONmessage (stack trace) toSystem.err.protected StringFormat a log message as a single string ready for printing.voidWrites anLog.Level.INFOmessage toSystem.out.voidWrites aLog.Level.WARNINGmessage toSystem.err.
-
Constructor Details
-
ConsoleLogger
public ConsoleLogger()
-
-
Method Details
-
info
Writes anLog.Level.INFOmessage toSystem.out.- Specified by:
infoin interfaceILogListener- Parameters:
message- the message; nevernull
-
config
Writes aLog.Level.CONFIGmessage toSystem.out.- Specified by:
configin interfaceILogListener- Parameters:
message- the message; nevernull
-
warning
Writes aLog.Level.WARNINGmessage toSystem.err.- Specified by:
warningin interfaceILogListener- Parameters:
message- the message; nevernull
-
error
Writes aLog.Level.ERRORmessage toSystem.err.- Specified by:
errorin interfaceILogListener- Parameters:
message- the message; nevernull
-
exception
Writes aLog.Level.EXCEPTIONmessage (stack trace) toSystem.err.- Specified by:
exceptionin interfaceILogListener- Parameters:
message- the formatted stack trace; nevernull
-
format
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 levelmessage- the raw message text- Returns:
- the formatted, ready-to-print string
-