Vorpal - Log
A Vorpal.js extension adding simple logging methods.
Installation
npm install vorpal-log
npm install vorpal
Getting Started
const vorpal = (require('vorpal'))();
const vorpalLog = require('vorpal-log');
vorpal.use(vorpalLog)
.delimiter('vorpal-log demo
Default Functionality
Vorpal-log comes with the following predefined methods for logging:
logger.debug(msg) (loglevel 10)
logger.log(msg) (loglevel 20)
logger.info(msg) (loglevel 20)
logger.confirm(msg) (loglevel 20)
logger.warn(msg) (loglevel 30)
logger.error(msg) (loglevel 40)
logger.fatal(msg) (loglevel 50)
logger.printMsg(msg) will always print the message without caring about the loglevel or formatting.
Set the loglevel:
logger.setFilter(level)
E.g. logger.setFilter('warn') will only print messages via logger.warn, logger.error and logger.fatal.
Options
The following options passed by vorpal.use(vorpalLogger, options) are used:
printDate: If this is true, the default formatters begin each message with the current date.
preformat: a function which is passed the message and which returns a string which should be logged instead by the default formatters. Could for example be used to render marked.
Commands
Vorpal-log adds the following (hidden) command, which simply delegates to logger.setFilter. As a user can enable debug logging with it, you might want to remove it for production.
Usage: loglevel [options] <level>
set the log level
Options:
--help output usage information
Customizing
logger.addFormatter(name, level, format)
Creates a logger[name] function, which will log the result of format(msg) with loglevel level. Use this to add custom formatters.
See customFormatter here for a working, insightful and useful example.
logger.setFilter(filter)
Takes either a custom filter function, a string, or a number.
When logging something, the formatter is passed to the function set by logger.setFilter. Logging only happens if this function returns true.
If logger.setFilter is passed a function, this function is the new filter function.
If passed a number, the new filter function will only log, if formatter.level is greater than or equal to that number.
If passed a string, the new filter function will only log if formatter.level is greater than or equal to formatters[filter].level.
)
.show();
const logger = vorpal.logger;
vorpal.command('log')
.action(function(args, cb) {
logger.debug('Log command called without arguments.');
logger.log('Foo, bar, baz!');
logger.confirm('You successfully ran the log command.');
logger.info('It logs stuff.');
logger.warn('Careful with that axe, Eugene!');
logger.error('Something went wrong...');
logger.fatal('If this was a real program, it would probably shut down now.');
cb();
});
logger.info('This is a demo program for the vorpal-log extension.');
logger.info('Run log to produce some output.');
logger.info('Run loglevel <level> to change the level, e.g. \'loglevel warn\'');
Default Functionality
Vorpal-log comes with the following predefined methods for logging:
- __INLINE_CODE_0__ (loglevel 10)
- __INLINE_CODE_1__ (loglevel 20)
- __INLINE_CODE_2__ (loglevel 20)
- __INLINE_CODE_3__ (loglevel 20)
- __INLINE_CODE_4__ (loglevel 30)
- __INLINE_CODE_5__ (loglevel 40)
- __INLINE_CODE_6__ (loglevel 50)
__INLINE_CODE_7__ will always print the message without caring about the loglevel or formatting.
Set the loglevel:
__INLINE_CODE_8__
E.g. __INLINE_CODE_9__ will only print messages via logger.warn, logger.error and logger.fatal.
Options
The following options passed by __INLINE_CODE_10__ are used:
- __INLINE_CODE_11__: If this is true, the default formatters begin each message with the current date.
- __INLINE_CODE_12__: a function which is passed the message and which returns a string which should be logged instead by the default formatters. Could for example be used to render marked.
Commands
Vorpal-log adds the following (hidden) command, which simply delegates to __INLINE_CODE_13__. As a user can enable debug logging with it, you might want to remove it for production.
__CODE_BLOCK_2__Customizing
logger.addFormatter(name, level, format)
Creates a __INLINE_CODE_14__ function, which will log the result of __INLINE_CODE_15__ with loglevel __INLINE_CODE_16__. Use this to add custom formatters.
See __INLINE_CODE_17__ here for a working, insightful and useful example.
logger.setFilter(filter)
Takes either a custom filter function, a string, or a number.
When logging something, the formatter is passed to the function set by __INLINE_CODE_18__. Logging only happens if this function returns true.
If __INLINE_CODE_19__ is passed a function, this function is the new filter function.
If passed a number, the new filter function will only log, if __INLINE_CODE_20__ is greater than or equal to that number.
If passed a string, the new filter function will only log if __INLINE_CODE_21__ is greater than or equal to __INLINE_CODE_22__.