creating easy, practical logging with instant implementation. this is the icing to your metaphorical cake!
npm i confectionery
For example, const logger = confectionery.createLogger("Logger");
allows the following:
logger.log("This is output as INFO");
logger.info("This is output as INFO", "Optional Context");
logger.warn("This is output as WARN");
logger.error("This is output as ERROR", "Optional Context");
logger.debug("This is output as DEBUG");
You can have multiple logger instances at a time.
A log level allows you to specify what gets printed to the console and logfiles.
In the below example, the console is set to debug and the logfile is set to error.
logger.setLevel("debug", "error");
In the below example, the both the console and the logfile are set to info.
logger.setLevel(3, 3);
The following are provided log levels:
Level | # | Default |
---|---|---|
SILENT | 0 | |
ERROR | 1 | |
WARN | 2 | |
INFO | 3 | X |
DEBUG | 4 |
confectionery allows you to save logs to files. By default, this is disabled. To enable it, provide a directory to save logfiles in.
For example, logger.setLogPath('./logs/');
Developers may provide a preferred format to use when logging in the console.
For example, logger.setFormat('classic');
.
Default templates (classic, short, symbols) can be found in scripts/stdout/templates.js
Custom formats are also supported. Simply use logger.setFormat();
with an object.
This object must contain functions that return debug, info, warn, and error.
Each message line is printed in ${line}
and context in ${context}
.
Moment can be used to format a timestamp. For example,
const customClassic = {
debug: function handle(moment, context, line) { return `{dim [${moment().format('HH:mm:ss:ms')}]} {bold {white DEBUG}}: {gray ${context}}${line}\n`},
info: function handle(moment, context, line) { return `{dim [${moment().format('HH:mm:ss:ms')}]} {bold {cyan INFO}}: {gray ${context}}${line}\n`},
warn: function handle(moment, context, line) { return `{dim [${moment().format('HH:mm:ss:ms')}]} {bold {yellow WARN}}: {gray ${context}}${line}\n`},
error: function handle(moment, context, line) { return `{dim [${moment().format('HH:mm:ss:ms')}]} {bold {red ERROR}}: {gray ${context}}${line}\n`}
}
logger.setFormat(customClassic);
Note: confectionery does NOT automatically add line breaks. Please use \n
when designing formats.
confectionery provides a formatter to use when there is no logger, or the message needs a special format (mainly used in other modules.)
Example:
confectionery.customPrint((arg1, arg2, arg3) => { return `{bold {white ${arg1}}} {gray ${arg2}} ${arg3}\n`}, "ARGUMENT 1", "ARGUMENT 2", "ARGUMENT 3");