-
Notifications
You must be signed in to change notification settings - Fork 10
/
logger.js
53 lines (42 loc) · 1.1 KB
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const chalk = require('chalk')
const DEFAULTS = {
verbose: false,
silent: true
}
class Logger {
constructor(actor, color = 'white') {
this.actor = actor
this.color = color
}
info(msg) {
if (!DEFAULTS.verbose) return
this.log(msg, '️ ', 'white')
}
success(msg) {
this.log(msg, '✅', 'green')
}
warn(msg) {
this.log(msg, '⚠️ ', 'yellow')
}
error(msg) {
this.log(msg, '🚨', 'red')
}
log(msg, emoji, color = 'white') {
if (DEFAULTS.silent) return
let formattedMessage = chalk.keyword(color)(`${emoji} ${this._stringify(msg)}`)
if (DEFAULTS.verbose) {
const formatedPrefix = chalk.keyword(this.color)(`[${this.actor}]`)
formattedMessage = `${formatedPrefix} ${formattedMessage}`
}
console.error(formattedMessage)
}
_stringify(obj) {
return (typeof obj === 'object') ? JSON.stringify(obj) : obj.toString()
}
}
module.exports = (actor, color) => new Logger(actor, color)
module.exports.Logger = Logger
module.exports.setDefaults = (silent, verbose) => {
DEFAULTS.silent = silent
DEFAULTS.verbose = verbose
}