Skip to content

Commit 5237fe2

Browse files
committed
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
1 parent 14555cc commit 5237fe2

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

readme.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ custom.success('Custom Success Log');
196196
<img alt="Default Loggers" src="media/override-defaults.png" width="65%">
197197
</div>
198198

199-
The `options` object can hold any of the following attributes: `disabled`, `interactive`, `logLevel`, `secrets`, `stream`, `scope` and `types`.
199+
The `options` object can hold any of the following attributes: `disabled`, `interactive`, `logLevels`, `logLevel`, `secrets`, `stream`, `scope` and `types`.
200200

201201
##### `disabled`
202202

@@ -219,12 +219,32 @@ Switches all loggers belonging to the created instance into the interactive mode
219219

220220
Sets the general logging level of the created instance. Can be one of the following:
221221

222-
- `'info'` - Displays all messages from all loggers.
223-
- `'timer'` - Displays messages only from the `time`, `timeEnd`, `debug`, `warn`, `error` & `fatal` loggers.
224-
- `'debug'` - Displays messages only from the `debug`, `warn`, `error` & `fatal` loggers.
222+
- `'debug'` - Displays all messages from all loggers.
223+
- `'info'` - Displays messages from all loggers except `debug` level.
224+
- `'timer'` - Displays messages only from the `time`, `timeEnd`, `warn`, `error` & `fatal` loggers.
225225
- `'warn'` - Displays messages only from the `warn`, `error` & `fatal` loggers.
226226
- `'error'` - Displays messages only from the `error` & `fatal` loggers.
227227

228+
#### `logLevels`
229+
230+
- Type: `Object`
231+
232+
Allows you to add or override log levels.
233+
234+
For example, a value of `{ silly: -1 }` will add a level of `silly`, which will have even lower priority than `debug`.
235+
236+
Default log levels are:
237+
238+
```json5
239+
{
240+
debug: 0,
241+
info: 1,
242+
timer: 2,
243+
warn: 3,
244+
error: 4
245+
}
246+
```
247+
228248
##### `secrets`
229249

230250
- Type: `(String|Number)[]`

src/signale.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ const pkgConf = require('pkg-conf');
88
const pkg = require('./../package.json');
99
const defaultTypes = require('./types');
1010

11+
const defaultLogLevels = {
12+
debug: 0,
13+
info: 1,
14+
timer: 2,
15+
warn: 3,
16+
error: 4
17+
};
18+
1119
const {green, grey, red, underline, yellow} = chalk;
1220

1321
let isPreviousLogInteractive = false;
@@ -19,6 +27,8 @@ class Signale {
1927
this._interactive = options.interactive || false;
2028
this._config = Object.assign(this.packageConfiguration, options.config);
2129
this._customTypes = Object.assign({}, options.types);
30+
this._customLogLevels = Object.assign({}, options.logLevels);
31+
this._logLevels = Object.assign({}, defaultLogLevels, this._customLogLevels);
2232
this._disabled = options.disabled || false;
2333
this._scopeName = options.scope || '';
2434
this._timers = options.timers || new Map();
@@ -50,6 +60,7 @@ class Signale {
5060
timers: this._timers,
5161
stream: this._stream,
5262
secrets: this._secrets,
63+
logLevels: this._customLogLevels,
5364
logLevel: this._generalLogLevel
5465
});
5566
}
@@ -87,16 +98,6 @@ class Signale {
8798
return underline(this._longestLabel);
8899
}
89100

90-
get _logLevels() {
91-
return {
92-
info: 0,
93-
timer: 1,
94-
debug: 2,
95-
warn: 3,
96-
error: 4
97-
};
98-
}
99-
100101
set configuration(configObj) {
101102
this._config = Object.assign(this.packageConfiguration, configObj);
102103
}
@@ -230,16 +231,18 @@ class Signale {
230231
}
231232
}
232233

234+
const colorize = type.color ? chalk[type.color] : x => x;
235+
233236
if (this._config.displayBadge && type.badge) {
234-
signale.push(chalk[type.color](this._padEnd(type.badge, type.badge.length + 1)));
237+
signale.push(colorize(this._padEnd(type.badge, type.badge.length + 1)));
235238
}
236239

237240
if (this._config.displayLabel && type.label) {
238241
const label = this._config.uppercaseLabel ? type.label.toUpperCase() : type.label;
239242
if (this._config.underlineLabel) {
240-
signale.push(chalk[type.color](this._padEnd(underline(label), this._longestUnderlinedLabel.length + 1)));
243+
signale.push(colorize(this._padEnd(underline(label), this._longestUnderlinedLabel.length + 1)));
241244
} else {
242-
signale.push(chalk[type.color](this._padEnd(label, this._longestLabel.length + 1)));
245+
signale.push(colorize(this._padEnd(label, this._longestLabel.length + 1)));
243246
}
244247
}
245248

@@ -346,7 +349,8 @@ class Signale {
346349
throw new Error('No scope name was defined.');
347350
}
348351

349-
return new Signale(Object.assign(this.currentOptions, {scope: name}));
352+
const SignaleConstructor = this.constructor || Signale;
353+
return new SignaleConstructor(Object.assign(this.currentOptions, {scope: name}));
350354
}
351355

352356
unscope() {

src/types.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ module.exports = {
1414
label: 'fatal',
1515
logLevel: 'error'
1616
},
17+
alert: {
18+
badge: figures('⬤'),
19+
color: 'red',
20+
label: 'alert',
21+
logLevel: 'error'
22+
},
1723
fav: {
1824
badge: figures('❤'),
1925
color: 'magenta',
@@ -81,8 +87,8 @@ module.exports = {
8187
logLevel: 'info'
8288
},
8389
debug: {
84-
badge: figures('⬤'),
85-
color: 'red',
90+
badge: figures.pointerSmall,
91+
color: '',
8692
label: 'debug',
8793
logLevel: 'debug'
8894
},

0 commit comments

Comments
 (0)