Create a Logger instance.
The typical use case is to create a logger with a given log level,
usually info
.
const log = new Logger({
level: 'info'
})
By default, the system console is used.
The complete use case is to create the logger instance with both a
console
and a level
. This might be particularly useful in tests,
where a mock console can be used to capture log messages.
const log = new Logger({
console: mockConsole,
level: 'info'
})
If present, the console
must be an object derived from the
node Console, possibly with some methods overridden.
The level
property is optional since it can be set later.
Without it, the constructor will
create the logger in a preliminary state, and all log lines will be stored
in an internal buffer until the log level is set.
const log = new Logger()
The generic object used to pass parameters to the constructor.
Optional
level?: LogLevelLog level.
The name of the log level; if not passed, the logger is created in a preliminary state, and all log lines will be stored in an internal buffer, until the log level is set.
Optional
console?: ConsoleUnderlying console.
The console object used to log the message;
by default, the JavaScript standard console
object is used.
Accessor to check the log level.
True if the log level is silent
or higher.
Accessor to check the log level.
True if the log level is error
or higher.
Accessor to check the log level.
True if the log level is warn
or higher.
Accessor to check the log level.
True if the log level is info
or higher.
Accessor to check the log level.
True if the log level is verbose
or higher.
Accessor to check the log level.
True if the log level is debug
or higher.
Accessor to check the log level.
True if the log level is trace
or higher.
Accessor to check the log level.
True if the log level is all
.
Accessor to check if the log level was initialised.
True if the level was set.
If the logger was created without an explicit log level, the logger is in a preliminary state and all log lines will be stored in an internal buffer until the log level is set.
if (!log.hasLevel) {
log.level = defaultLevel
}
Accessor to get the log level.
The log level name.
Get the current log level, as a string.
console.log(log.level)
Accessor to set the log level.
Set the log level. If this is the first time when the log level is set, flush the internal buffer.
log.level = 'info'
If the log level is not one of the known strings, an assert will fire.
The new log level.
Accessor to get the underlying console
object.
The console object used by the logger.
Direct access to the console object is useful in tests, when the console is a mock object, which allows to check the logged messages.
Check if the log level is set to a given level name.
True if the current log level is equal to the given level or higher.
This is a more generic version of the accessors (like isDebug
, etc),
to be used when the log level is not know at compile time.
It can also be used to ensure that the log level is not decreased, for example:
if (!log.islevel(newLevel)) {
log.level = newLevel
}
The name of the log level.
Protected
writeThe internal log writer.
If the log level was defined, call the function, otherwise store the log line details in the array buffer, for later processing, when the log level is defined.
The log numeric level.
The function to be used to write the message.
The log message.
Log a message.
Log the message always, regardless of the log level, (even 'silent'
,
when no other messages are logged).
The message is passed via console.log()
.
log.always(version)
Message to log, as accepted by util.format()
.
Rest
...args: any[]Optional variable arguments.
Log an error message.
Log a message if the log level is error
or higher.
The message is prefixed with error:
and
passed via console.error()
.
log.error('Not good...')
There is a special case when the input is an Error
object. It
is expanded, including a full stack trace, and passed via
console.error()
.
try {
// ...
} catch (err) {
log.error(err)
}
Message to log, as accepted by util.format()
.
Rest
...args: any[]Optional variable arguments.
Log an error message.
Log a message if the log level is error
or higher.
It differs from error()
by not prefixing the string with error:
and using console.log()
instead of console.error()
.
log.output('Not good either...')
try {
// ...
} catch (err) {
// Do not show the stack trace.
log.output(err)
}
Message to log, as accepted by util.format()
.
Rest
...args: any[]Optional variable arguments.
Log a warning message.
Log a message if the log level is warn
or higher.
The message is prefixed with warning:
and
passed via console.error()
.
log.info(title)
Message to log, as accepted by util.format()
.
Rest
...args: any[]Optional variable arguments.
Log an informative message.
Log a message if the log level is info
or higher.
The message is passed via console.log()
.
log.info(title)
Message to log, as accepted by util.format()
.
Rest
...args: any[]Optional variable arguments.
Log a verbose message.
Log a message if the log level is verbose
or higher.
The message is passed via console.log()
.
log.verbose('Configurations:')
Message to log, as accepted by util.format()
.
Rest
...args: any[]Optional variable arguments.
Log a debug message.
Log a message if the log level is 'debug'
or higher.
The message is prefixed with debug:
and
passed via console.log()
.
log.debug(`spawn: ${cmd}`)
Message to log, as accepted by util.format()
.
Rest
...args: any[]Optional variable arguments.
Log a trace message.
Log a message if the log level is trace
or higher.
The message is prefixed with trace:
and
passed via console.log()
.
log.trace(`${this.constructor.name}.doRun()`)
Message to log, as accepted by util.format()
.
Rest
...args: any[]Optional variable arguments.
Protected
Readonly
_consoleThe console object used to output the log messages.
Protected
levelThe numerical value of the log level.
Protected
levelThe name of the log level.
Protected
bufferEmpty buffer where preliminary log lines are stored until the log level is set.
Static
defaultThe recommended default level.
Static
numericInternal numerical values for the log level.
Static
numericThe value used for the undefined log level (maximum value).
Static
numericThe value used for the always
case (minimum value).
Generated using TypeDoc
Summary
The Logger class implements the logger functionality.
Description
The logger is constructed on top of a console object, where the messages are logged.
Use
log.always()
instead of theconsole.log()
, since it accounts for different contexts, created for example when using REPL.There is no
critical
level, corresponding to errors that prevent the program to run, since these are actually related to bugs; useassert()
instead.The messages may include formatting directives, with additional arguments, as defined by the Node.js console (not really necessary with ES6).
All output functions accept an optional string message and possibly some arguments, as processed by the standard Node.js
util.format(msg, ...args)
function.If the logging code is more complex than a single line, for example if it needs a long loop, it is recommended to explicitly check the log level and, if not high enough, skip the code entirely.
Example
There are cases when the logger must be created very early in the life cycle of an application, even before it is practically possible to determine the log level.
For these cases, if the logger is created without a log level, it is set to a preliminary state, and all log lines are stored in an internal buffer, until the log level is set, when the buffer is walked and the lines are processed.