Skip to content

Commit

Permalink
feat: add new method addLevel() to LambdaLog
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleRoss committed Apr 12, 2021
1 parent 2c270e5 commit 9fde2e3
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions lib/LambdaLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,37 @@ class LambdaLog extends EventEmitter {
*/
this.console = this.options.logHandler;

this._levels.forEach(lvl => {
/**
* Shortcut methods for `log.log()`. By default, the following methods are available: `log.info()`, `log.warn()`, `log.error()` and `log.debug()`.
* Additional methods will be added for any [custom log levels](#custom-log-levels) provided.<br><br>The provided msg can be any type, although a string
* or `Error` is recommended. If `Error` is provided the stack trace is added as metadata to the log as `stack`.
* @alias LambdaLog#&lt;info|warn|error|debug|*&gt;
* @param {*} msg Message to log. Can be any type, but string or `Error` reccommended.
* @param {Object} [meta={}] Optional meta data to attach to the log.
* @param {Array} [tags=[]] Additional tags to append to this log.
* @return {LogMessage} The LogMessage instance for the log.
*
* @example
* const log = require('lambda-log');
*
* log.info('Test info log');
* log.debug('Test debug log');
* log.warn('Test warn log');
* log.error('Test error log');
*/
this[lvl] = (msg, meta = {}, tags = []) => {
return this.log(lvl, msg, meta, tags);
};
});
const levelsConfig = this[symbols.LEVELS];
for(const lvl in levelsConfig) {
if(Object.prototype.hasOwnProperty.call(levelsConfig, lvl)) {
this.addLevel(lvl, levelsConfig[lvl]);
}
}
}

/**
* Add a new log level to this instance of LambdaLog.
* @since 3.0.0
* @param {String} name The name of the new log level.
* @param {String|(message: LogMessage) => String} handler The string name of the `console` method to call or a function that returns a string method name.
*/
addLevel(name, handler) {
this[symbols.LEVELS][name] = handler;

/**
* Shortcut methods for `log.log()`. By default, the following methods are available: `log.info()`, `log.warn()`, `log.error()` and `log.debug()`.
* Additional methods will be added for any [custom log levels](#custom-log-levels) provided.<br><br>The provided msg can be any type, although a string
* or `Error` is recommended. If `Error` is provided the stack trace is added as metadata to the log as `stack`.
* @param {*} msg Message to log. Can be any type, but string or `Error` reccommended.
* @param {Object} [meta={}] Optional meta data to attach to the log.
* @param {String[]} [tags=[]] Additional tags to append to this log.
* @return {LogMessage} The LogMessage instance for the log.
*/
this[name] = (msg, meta = {}, tags = []) => {
return this.log(name, msg, meta, tags);
};

return this;
}

/**
Expand Down

0 comments on commit 9fde2e3

Please sign in to comment.