Table of Content
Quick Start
No bells and whistles, just a simple logger!
The module logtrine
is an object-oriented logger, based on an interface and currently providing two implementations:
- The
ConsoleLogger
which writes messages to the standard output - The
FileLogger
which writes messages to a log file
Features:
- A log level can be set at any time to limit/filter the written log message types
- Each log message is prefixed with the ISO time and a tag indicating the log level
- Loggers can be 'chained' to write a log message to multiple targets with a single call
ConsoleLogger
// Include the module and create an instance with debug level <Info>const ConsoleLogger = ;var logger = ConsoleLoggerLEVELInfo; // This will be shown on the terminal in default colorlogger; // This will be shown on the terminal in redlogger; // This will not be shown, because the logger was initialized with log level <Info>logger; // Lets change the log level on the flyloggerlevel = ConsoleLoggerLEVELDebug; // Now debug message written with this logger instance will be shownlogger;
Output:
FileLogger
// Include the module and create an instance with debug level <Info>const FileLogger = ;var logger = '/tmp/logtrine.log' FileLoggerLEVELInfo; // This will delete all content of the log fileloggerclear; // This will add a log message to the log filelogger; // This will add a log message to the log filelogger; // This will not be added, because the logger was initialized with log level <Info>logger; // Lets change the log level on the flyloggerlevel = FileLoggerLEVELDebug; // Now debug message written with this logger instance will be addedlogger;
Output:
General
...
Log Levels
The following log levels are available:
const ConsoleLogger = ;var logger = ; // default level is <Info> // Only messages that are logged with a level equal or higher// to the current log level will be shown// The following log levels are ordered ascendingloggerlevel = ConsoleLoggerLEVELAll;loggerlevel = ConsoleLoggerLEVELTrace;loggerlevel = ConsoleLoggerLEVELDebug;loggerlevel = ConsoleLoggerLEVELVerbose;loggerlevel = ConsoleLoggerLEVELInfo;loggerlevel = ConsoleLoggerLEVELWarn;loggerlevel = ConsoleLoggerLEVELError;loggerlevel = ConsoleLoggerLEVELCritical;loggerlevel = ConsoleLoggerLEVELNone; // disables all log messages
Log Methods
The following methods can be used to log data for the corresponding level. Each method supports a variable number of input parameters of any type:
const ConsoleLogger = ;var logger = ConsoleLoggerLEVELAll; logger;logger;logger;logger;logger;logger;logger;
Log Chaining
Sometimes a log message shall be logged to multiple targets (e.g. console + file or fileA + fileB). This is where logger chaining comes into the picture.
// Include the module and create an instance with log level <Info>const ConsoleLogger FileLogger = ;var logger = ConsoleLoggerLEVELInfo '/tmp/logtrine.log'; // The chained logger can be accessed through the logger property// Lets clear the log file of the chained loggerloggerloggerclear; // This message will be written to the console and to the log filelogger; // Changing a level will also affect a level change on all chained loggersloggerlevel = FileLoggerLEVELDebug; // This will appear on the console and in the log filelogger; // If you really want the chained logger to have a different log level set it afterwardsloggerloggerlevel = FileLoggerLEVELWarn; // This will appear on the console, but not in the log filelogger; // A chained logger can be detached by setting the null referenceloggerlogger = null; // No chained logger exists anymore, output only to consolelogger;