-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
125 lines (103 loc) · 2.9 KB
/
index.js
File metadata and controls
125 lines (103 loc) · 2.9 KB
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
const path = require('path')
, vsprintf = require('sprintf-js').vsprintf
, sprintf = require('sprintf-js').sprintf
, time = require('./lib/time')
, c = require('ansi-colors')
, caller = require('caller')
, generator = require('./lib/generator')
let logDirectory = ''
function Logger(name, basefile) {
this.name = name || ''
this.defaultArgsFormat = []
this.logTypes = ['info', 'error', 'warn', 'debug']
this.colorMap = {
info: 'blue',
error: 'red',
warn: 'yellow',
debug: 'cyan'
}
this.writer = {
info: 'stdout',
error: 'stderr',
warn: 'stderr',
debug: 'stderr'
}
this.basedir = path.dirname(basefile)
this.debugEnv = process.env.DEBUG
this.setup()
}
Logger.prototype.setup = function() {
if (this.debugEnv && this.debugEnv != 'true') {
this.debugEnv = this.debugEnv.split(',')
}
if (logDirectory) {
logDirectory = path.resolve(this.basedir, logDirectory)
this._createRotateLogger()
} else {
this._createAnsiColorLogger()
}
}
Logger.prototype._createRotateLogger = function() {
let logStream = require('rotating-file-stream')(generator, {
path: logDirectory,
interval: '1d',
compress: true
})
this.logTypes.forEach(msgType => {
this[msgType] = function() {
let msg = sprintf.apply(null, arguments)
, fmtArgs = this._createFormatArgs(msgType, msg)
if (this.debugEnv && msgType == 'debug') {
if (Array.isArray(this.debugEnv) && this.debugEnv.indexOf(fmtArgs.args[2]) == -1) {
return
}
}
logStream.write(vsprintf(fmtArgs.format, fmtArgs.args))
}.bind(this)
})
}
Logger.prototype._createAnsiColorLogger = function() {
this.logTypes.forEach(msgType => {
this[msgType] = function() {
let msg = sprintf.apply(null, arguments)
, fmtArgs = this._createFormatArgs(msgType, msg)
, appName = fmtArgs.args[2]
fmtArgs.args[0] = c.green(fmtArgs.args[0])
fmtArgs.args[1] = c[this.colorMap[msgType]](fmtArgs.args[1])
fmtArgs.args[2] = c.magenta(appName)
if (this.debugEnv && msgType == 'debug') {
if (Array.isArray(this.debugEnv) && this.debugEnv.indexOf(appName) == -1) {
return
}
}
process[this.writer[msgType]].write(vsprintf(fmtArgs.format, fmtArgs.args))
}.bind(this)
})
}
Logger.prototype._createFormatArgs = function(msgType, msg) {
let logName = this.name
if (!logName) {
logName = caller(2)
// Simplified path
logName = logName
.replace(this.basedir, '')
.replace(new RegExp(path.sep), '')
}
msg = (msg + '') + '\r\n'
return {
format: '[%s:%s:%s] %s',
args: [
time(),
msgType.toUpperCase(),
logName,
msg
]
}
}
function loggerFactory(name) {
return new Logger(name, caller())
}
loggerFactory.setDirectory = function(logDir) {
logDirectory = logDir
}
module.exports = loggerFactory