Skip to content

Commit

Permalink
Reordered logging levels, added alert logger
Browse files Browse the repository at this point in the history
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 "clone" 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
  • Loading branch information
anru committed Apr 8, 2020
1 parent 14555cc commit c35afb1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
32 changes: 26 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ custom.success('Custom Success Log');
<img alt="Default Loggers" src="media/override-defaults.png" width="65%">
</div>

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`

Expand All @@ -219,11 +219,31 @@ 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.
- `'warn'` - Displays messages only from the `warn`, `error` & `fatal` loggers.
- `'error'` - Displays messages only from the `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`, `alert` & `fatal` loggers.
- `'warn'` - Displays messages only from the `warn`, `error`, `alert` & `fatal` loggers.
- `'error'` - Displays messages only from the `error`, `alert` & `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`

Expand Down
32 changes: 18 additions & 14 deletions src/signale.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -50,6 +60,7 @@ class Signale {
timers: this._timers,
stream: this._stream,
secrets: this._secrets,
logLevels: this._customLogLevels,
logLevel: this._generalLogLevel
});
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)));
}
}

Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 8 additions & 2 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ module.exports = {
label: 'fatal',
logLevel: 'error'
},
alert: {
badge: figures('⬤'),
color: 'red',
label: 'alert',
logLevel: 'error'
},
fav: {
badge: figures('❤'),
color: 'magenta',
Expand Down Expand Up @@ -81,8 +87,8 @@ module.exports = {
logLevel: 'info'
},
debug: {
badge: figures('⬤'),
color: 'red',
badge: figures.pointerSmall,
color: '',
label: 'debug',
logLevel: 'debug'
},
Expand Down

0 comments on commit c35afb1

Please sign in to comment.