Skip to content

Commit

Permalink
Added support for log levels. Fixes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
klaudiosinani committed Feb 25, 2019
1 parent 7d79cfa commit b9e7016
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
30 changes: 23 additions & 7 deletions signale.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Signale {
this._stream = options.stream || process.stdout;
this._longestLabel = this._getLongestLabel();
this._secrets = options.secrets || [];
this._generalLogLevel = this._validateLogLevel(options.logLevel);

Object.keys(this._types).forEach(type => {
this[type] = this._logger.bind(this, type);
Expand All @@ -47,7 +48,8 @@ class Signale {
interactive: this._interactive,
timers: this._timers,
stream: this._stream,
secrets: this._secrets
secrets: this._secrets,
logLevel: this._generalLogLevel
});
}

Expand Down Expand Up @@ -82,6 +84,16 @@ 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 All @@ -100,6 +112,10 @@ class Signale {
return labels.reduce((x, y) => x.length > y.length ? x : y);
}

_validateLogLevel(level) {
return Object.keys(this._logLevels).includes(level) ? level : 'info';
}

_mergeTypes(standard, custom) {
const types = Object.assign({}, standard);

Expand Down Expand Up @@ -264,18 +280,18 @@ class Signale {
isPreviousLogInteractive = this._interactive;
}

_log(message, streams = this._stream) {
if (this.isEnabled()) {
_log(message, streams = this._stream, logLevel) {
if (this.isEnabled() && this._logLevels[logLevel] >= this._logLevels[this._generalLogLevel]) {
this._formatStream(streams).forEach(stream => {
this._write(stream, message);
});
}
}

_logger(type, ...messageObj) {
const {stream} = this._types[type];
const {stream, logLevel} = this._types[type];
const message = this._buildSignale(this._types[type], ...messageObj);
this._log(this._filterSecrets(message), stream);
this._log(this._filterSecrets(message), stream, this._validateLogLevel(logLevel));
}

_padEnd(str, targetLength) {
Expand Down Expand Up @@ -351,7 +367,7 @@ class Signale {
}

message.push('Initialized timer...');
this._log(message.join(' '));
this._log(message.join(' '), this._stream, 'timer');

return label;
}
Expand Down Expand Up @@ -379,7 +395,7 @@ class Signale {

message.push('Timer run for:');
message.push(yellow(span < 1000 ? span + 'ms' : (span / 1000).toFixed(2) + 's'));
this._log(message.join(' '));
this._log(message.join(' '), this._stream, 'timer');

return {label, span};
}
Expand Down
51 changes: 34 additions & 17 deletions types.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,103 @@ module.exports = {
error: {
badge: figures.cross,
color: 'red',
label: 'error'
label: 'error',
logLevel: 'error'
},
fatal: {
badge: figures.cross,
color: 'red',
label: 'fatal'
label: 'fatal',
logLevel: 'error'
},
fav: {
badge: figures('❤'),
color: 'magenta',
label: 'favorite'
label: 'favorite',
logLevel: 'info'
},
info: {
badge: figures.info,
color: 'blue',
label: 'info'
label: 'info',
logLevel: 'info'
},
star: {
badge: figures.star,
color: 'yellow',
label: 'star'
label: 'star',
logLevel: 'info'
},
success: {
badge: figures.tick,
color: 'green',
label: 'success'
label: 'success',
logLevel: 'info'
},
wait: {
badge: figures.ellipsis,
color: 'blue',
label: 'waiting'
label: 'waiting',
logLevel: 'info'
},
warn: {
badge: figures.warning,
color: 'yellow',
label: 'warning'
label: 'warning',
logLevel: 'warn'
},
complete: {
badge: figures.checkboxOn,
color: 'cyan',
label: 'complete'
label: 'complete',
logLevel: 'info'
},
pending: {
badge: figures.checkboxOff,
color: 'magenta',
label: 'pending'
label: 'pending',
logLevel: 'info'
},
note: {
badge: figures.bullet,
color: 'blue',
label: 'note'
label: 'note',
logLevel: 'info'
},
start: {
badge: figures.play,
color: 'green',
label: 'start'
label: 'start',
logLevel: 'info'
},
pause: {
badge: figures.squareSmallFilled,
color: 'yellow',
label: 'pause'
label: 'pause',
logLevel: 'info'
},
debug: {
badge: figures('⬤'),
color: 'red',
label: 'debug'
label: 'debug',
logLevel: 'debug'
},
await: {
badge: figures.ellipsis,
color: 'blue',
label: 'awaiting'
label: 'awaiting',
logLevel: 'info'
},
watch: {
badge: figures.ellipsis,
color: 'yellow',
label: 'watching'
label: 'watching',
logLevel: 'info'
},
log: {
badge: '',
color: '',
label: ''
label: '',
logLevel: 'info'
}
};

0 comments on commit b9e7016

Please sign in to comment.