forked from xiaodoudou/PlexIPTV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
173 lines (153 loc) · 4.76 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const packageJson = require('./package.json')
const _ = require('lodash')
if (_.get(process, 'env.DEBUG', false) === false) {
_.set(process, 'env.DEBUG', `${packageJson.name}:*:info,${packageJson.name}:*:warn,${packageJson.name}:*:error,${packageJson.name}:*:verbose`)
}
const colors = require('colors')
const createDebug = require('debug')
const filendir = require('filendir')
const mainDirectory = process.cwd()
const Moment = require('moment')
const path = require('path')
const rotate = require('log-rotate')
const stackTrace = require('stack-trace')
const util = require('util')
const args = require('args')
const flags = args.parse(process.argv)
const logPath = `${flags.logdir}/logs.txt`
class Logger {
constructor () {
// Declares
const trace = stackTrace.get()
trace.splice(0, 1)
const filename = path.basename(_.first(trace).getFileName(), path.extname(_.first(trace).getFileName())).replace(`${mainDirectory}${path.sep}`, '')
const namespace = `${packageJson.name}:${filename}`
const color = this.selectColor(filename)
this.proceedToRotate = false
this._trace = {
value: '👣',
instance: createDebug(`${namespace}:trace`)
}
this._trace.color = color
this._trace.instance.log = this.log.bind(this)
this._verbose = {
prefix: '🔭',
instance: createDebug(`${namespace}:verbose`)
}
this._verbose.color = color
this._verbose.instance.log = this.log.bind(this)
this._debug = {
prefix: '🐛',
instance: createDebug(`${namespace}:debug`)
}
this._debug.color = color
this._debug.instance.log = this.log.bind(this)
this._info = {
prefix: '📟',
instance: createDebug(`${namespace}:info`)
}
this._info.color = color
this._info.instance.log = this.log.bind(this)
this._warn = {
prefix: '⚠️',
instance: createDebug(`${namespace}:warn`)
}
this._warn.color = color
this._warn.instance.log = this.log.bind(this)
this._error = {
prefix: '🔥',
instance: createDebug(`${namespace}:error`)
}
this._error.color = color
this._error.instance.log = this.log.bind(this)
// Bindings
this.selectColor = this.selectColor.bind(this)
this.write = this.write.bind(this)
this.write = this.write.bind(this)
this.timestamp = this.timestamp.bind(this)
this.template = this.template.bind(this)
this.trace = this.trace.bind(this)
this.verbose = this.verbose.bind(this)
this.debug = this.debug.bind(this)
this.debug = this.debug.bind(this)
this.info = this.info.bind(this)
this.warn = this.warn.bind(this)
this.error = this.error.bind(this)
}
selectColor (namespace) {
let hash = 0
let i
for (i in namespace) {
hash = ((hash << 5) - hash) + namespace.charCodeAt(i)
hash |= 0
}
return createDebug.colors[Math.abs(hash) % createDebug.colors.length]
}
log (...args) {
if (!this.proceedToRotate) {
rotate(logPath, { count: 3 }, () => {
this.proceedToRotate = true
this.write(util.format.apply(util, args))
})
} else {
this.write(util.format.apply(util, args))
}
}
write (...args) {
process.stderr.write(util.format.apply(util, args) + '\n')
filendir.writeFile(logPath, `${util.format.apply(util, args)}\n`, { flag: 'a+' }, (error) => {
if (error) {
process.stderr.write("Didn't succeed to write logs")
process.exit()
}
})
}
timestamp () {
return (new Moment()).format('YYYY/MM/DD HH:mm:ss.SSSS')
}
template (logLevel, data) {
const trace = stackTrace.get()
trace.splice(0, 2)
let methodName = ''
_.forEach(trace, (item) => {
if (item.getFunctionName() != null) {
methodName = item.getFunctionName()
return false
}
})
methodName = colors.green(methodName)
if (logLevel == null) { logLevel = this._trace }
const prefix = `${this.timestamp()} ${logLevel.prefix} ${methodName}`
if (_.isArray(data) && data.length === 1) { data = _.first(data) }
if (_.isString(data)) {
logLevel.instance('%s %s', prefix, data)
} else {
if (_.isString(_.first(data))) {
const logMessage = _.first(data)
data.splice(0, 1)
logLevel.instance('%s %s\n%O', prefix, logMessage, data)
} else {
logLevel.instance('%s\n%O', prefix, data)
}
}
}
trace (...data) {
return this.template(this._trace, data)
}
verbose (...data) {
return this.template(this._verbose, data)
}
debug (...data) {
return this.template(this._debug, data)
}
info (...data) {
return this.template(this._info, data)
}
warn (...data) {
return this.template(this._warn, data)
}
error (...data) {
return this.template(this._error, data)
}
}
module.exports = Logger