-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.js
89 lines (81 loc) · 1.99 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
'use strict'
const kc = require('./kafclient.js')
/* understand/
* our logger works by logging "events"
* log("mymodule/did/something")
* the log can contain additional data
* log("mymodule/did/athing", helpful_data)
*
* all error events should start with "err/"
* log("err/inmymodule", e)
*
* from the perspective of this kind of app - the only
* logging levels we need are
* (a) the actual logs - always tracked
* and
* (b) trace logs - helpful while debugging
*
* hence the logger allows us to call trace() which
* is only enabled when the logger is initialized with
* `traceOn`
* log.trace("trace/mymodule/did/something", debuginfo)
*/
module.exports = (LOGNAME, traceOn) => {
/* way/
* log the event by posting it to the logfile, handling
* error objects correctly by capturing their most useful
* info (stacktrace)
*/
function log(e, data, cb) {
if(!cb && typeof data === 'function') {
cb = data
data = undefined
}
let msg
let t = (new Date()).toISOString()
if(data) {
if(data instanceof Error) {
msg = { t, e, data: data.stack }
} else {
msg = { t, e, data }
}
} else {
msg = { t, e }
}
kc.put(msg, LOGNAME, cb)
}
/* understand/
* all trace logs should start with "trace/" for easy
* identification
*/
function trace(e, data, cb) {
if(!cb && typeof data === 'function') {
cb = data
data = undefined
}
if(!data && typeof e === "object") {
data = e
e = ""
}
if(!e) e = "trace/"
else if(typeof e !== 'object') e = `trace/${e}`
log(e, data, cb)
}
/* understand/
* ignore trace logs when not enabled
*/
function silentlyIgnore(e, data, cb) {
if(!cb && typeof data === 'function') {
cb = data
data = undefined
}
cb && cb()
}
log.getName = () => LOGNAME
if(traceOn) {
log.trace = trace
} else {
log.trace = trace /*TODO: silentlyIgnore */
}
return log
}