From 5237fe257facbb87dc648addac6efc45df1bfa36 Mon Sep 17 00:00:00 2001 From: Andrey Rublev Date: Wed, 8 Apr 2020 12:35:08 +0700 Subject: [PATCH] Reordered logging levels, added alert logger What's inside: 1. Added the "alert" logger, it is intended for messages that must be seen, for example, to manually catch an error. 2. The "debug" level now has a low priority and is not so visually highlighted: it is intended for unimportant messages that are usually not needed, but which would be useful to have in the system if something goes wrong. 3. In case someone wants to inherit from the "Signale" class, the "scope" method now correctly creates an instance of the current class with which it was created. 4. Added the ability to set your own log levels, without having to inherit to override the "_logLevels" getter. Fixes #96 --- readme.md | 28 ++++++++++++++++++++++++---- src/signale.js | 32 ++++++++++++++++++-------------- src/types.js | 10 ++++++++-- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/readme.md b/readme.md index 93f9c332..04038aae 100644 --- a/readme.md +++ b/readme.md @@ -196,7 +196,7 @@ custom.success('Custom Success Log'); Default Loggers -The `options` object can hold any of the following attributes: `disabled`, `interactive`, `logLevel`, `secrets`, `stream`, `scope` and `types`. +The `options` object can hold any of the following attributes: `disabled`, `interactive`, `logLevels`, `logLevel`, `secrets`, `stream`, `scope` and `types`. ##### `disabled` @@ -219,12 +219,32 @@ Switches all loggers belonging to the created instance into the interactive mode Sets the general logging level of the created instance. Can be one of the following: -- `'info'` - Displays all messages from all loggers. -- `'timer'` - Displays messages only from the `time`, `timeEnd`, `debug`, `warn`, `error` & `fatal` loggers. -- `'debug'` - Displays messages only from the `debug`, `warn`, `error` & `fatal` loggers. +- `'debug'` - Displays all messages from all loggers. +- `'info'` - Displays messages from all loggers except `debug` level. +- `'timer'` - Displays messages only from the `time`, `timeEnd`, `warn`, `error` & `fatal` loggers. - `'warn'` - Displays messages only from the `warn`, `error` & `fatal` loggers. - `'error'` - Displays messages only from the `error` & `fatal` loggers. +#### `logLevels` + +- Type: `Object` + +Allows you to add or override log levels. + +For example, a value of `{ silly: -1 }` will add a level of `silly`, which will have even lower priority than `debug`. + +Default log levels are: + +```json5 +{ + debug: 0, + info: 1, + timer: 2, + warn: 3, + error: 4 +} +``` + ##### `secrets` - Type: `(String|Number)[]` diff --git a/src/signale.js b/src/signale.js index 93960c3a..cdbe334b 100644 --- a/src/signale.js +++ b/src/signale.js @@ -8,6 +8,14 @@ const pkgConf = require('pkg-conf'); const pkg = require('./../package.json'); const defaultTypes = require('./types'); +const defaultLogLevels = { + debug: 0, + info: 1, + timer: 2, + warn: 3, + error: 4 +}; + const {green, grey, red, underline, yellow} = chalk; let isPreviousLogInteractive = false; @@ -19,6 +27,8 @@ class Signale { this._interactive = options.interactive || false; this._config = Object.assign(this.packageConfiguration, options.config); this._customTypes = Object.assign({}, options.types); + this._customLogLevels = Object.assign({}, options.logLevels); + this._logLevels = Object.assign({}, defaultLogLevels, this._customLogLevels); this._disabled = options.disabled || false; this._scopeName = options.scope || ''; this._timers = options.timers || new Map(); @@ -50,6 +60,7 @@ class Signale { timers: this._timers, stream: this._stream, secrets: this._secrets, + logLevels: this._customLogLevels, logLevel: this._generalLogLevel }); } @@ -87,16 +98,6 @@ class Signale { return underline(this._longestLabel); } - get _logLevels() { - return { - info: 0, - timer: 1, - debug: 2, - warn: 3, - error: 4 - }; - } - set configuration(configObj) { this._config = Object.assign(this.packageConfiguration, configObj); } @@ -230,16 +231,18 @@ class Signale { } } + const colorize = type.color ? chalk[type.color] : x => x; + if (this._config.displayBadge && type.badge) { - signale.push(chalk[type.color](this._padEnd(type.badge, type.badge.length + 1))); + signale.push(colorize(this._padEnd(type.badge, type.badge.length + 1))); } if (this._config.displayLabel && type.label) { const label = this._config.uppercaseLabel ? type.label.toUpperCase() : type.label; if (this._config.underlineLabel) { - signale.push(chalk[type.color](this._padEnd(underline(label), this._longestUnderlinedLabel.length + 1))); + signale.push(colorize(this._padEnd(underline(label), this._longestUnderlinedLabel.length + 1))); } else { - signale.push(chalk[type.color](this._padEnd(label, this._longestLabel.length + 1))); + signale.push(colorize(this._padEnd(label, this._longestLabel.length + 1))); } } @@ -346,7 +349,8 @@ class Signale { throw new Error('No scope name was defined.'); } - return new Signale(Object.assign(this.currentOptions, {scope: name})); + const SignaleConstructor = this.constructor || Signale; + return new SignaleConstructor(Object.assign(this.currentOptions, {scope: name})); } unscope() { diff --git a/src/types.js b/src/types.js index 2d57d555..67c8080b 100644 --- a/src/types.js +++ b/src/types.js @@ -14,6 +14,12 @@ module.exports = { label: 'fatal', logLevel: 'error' }, + alert: { + badge: figures('⬤'), + color: 'red', + label: 'alert', + logLevel: 'error' + }, fav: { badge: figures('❤'), color: 'magenta', @@ -81,8 +87,8 @@ module.exports = { logLevel: 'info' }, debug: { - badge: figures('⬤'), - color: 'red', + badge: figures.pointerSmall, + color: '', label: 'debug', logLevel: 'debug' },