-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (87 loc) · 2.86 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict'
const chalk = require('chalk')
const prettyjson = require('prettyjson')
const safeStringify = require('fast-safe-stringify')
const DateFormat = require('fast-date-format')
const dateFormat = new DateFormat('YYYY-MM-DD HH:mm:ss')
const options = {
keysColor: 'yellow',
stringColor: 'green',
numberColor: 'red'
}
class Logger {
constructor (name, options = {}) {
if (typeof name !== 'string' || name === '') {
throw new Error('You must supply a name when creating a logger.')
}
this.name = name.toUpperCase()
this.level = options.level || 'trace'
this.throwOnError = options.throwOnError || false // Exit when error occures, be careful if you exit early not all errors are printed
this.colorEntireLine = options.colorEntireLine || false // color the entire log line, if set to false only meta will be colored
this.levels = ['trace', 'debug', 'info', 'warn', 'error', 'off']
this.colors = ['grey', 'cyan', 'greenBright', 'yellowBright', 'red', 'grey']
}
_getColor (level) {
return this.colors[this.levels.indexOf(level)]
}
_shouldPrint (level) {
return this.levels.indexOf(level) >= this.levels.indexOf(this.level)
}
_write (name, level, msgs) {
if (this._shouldPrint(level)) {
let logLine = ''
for (const msg of msgs) {
// Do not stringify Error instances
if (msg instanceof Error) {
logLine += '\n ' + msg.stack
continue
}
if (typeof msg === 'object') {
logLine += ' ' + safeStringify(msg)
continue
}
logLine += ' ' + msg
}
const color = this._getColor(level)
const levelStr = level.toUpperCase()
const timestamp = dateFormat.format() // It will take current timestamp is no date is passed
const meta = `${timestamp} [${levelStr}] [${name}]`
let finalString = chalk[color](meta) + `${logLine}`
if (this.colorEntireLine) {
finalString = chalk[color](`${meta} ${logLine}`)
}
console.log(finalString) // for further extreme performance, write directly to stdout. (aware of the formatting tradeoffs)
// process.stdout.write(finalString + '\n');
}
}
setLogLevel (level) {
this.level = level.toLowerCase()
return this
}
getLogLevel () {
return this.level
}
error (...msgs) {
this._write(this.name, this.error.name, msgs)
if (this.throwOnError) {
throw new Error(msgs)
}
}
warn (...msgs) {
this._write(this.name, this.warn.name, msgs)
}
info (...msgs) {
this._write(this.name, this.info.name, msgs)
}
debug (...msgs) {
this._write(this.name, this.debug.name, msgs)
}
trace (...msgs) {
this._write(this.name, this.trace.name, msgs)
}
// Pretty print, for debugging, not performant
pp (obj) {
console.log(prettyjson.render(obj, options))
}
}
module.exports = Logger