-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjsonOutput.cjs
122 lines (110 loc) · 2.51 KB
/
jsonOutput.cjs
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
const { Log } = require('..')
Log.options({ level: 'DEBUG', json: true, serverinfo: true, spaces: 2 })
const log = new Log('package')
// ----
// use formatters (NOT RECOMMENDED for objects, errors)
log.debug('a %s, a number %d, an %o and %j', 'string', 1.2, { object: 1 }, { NOT: 'RECOMMENDED' })
/* > {
"level": "DEBUG",
"name": "package",
"msg": "a string, a number 1.2, an {\n \"object\": 1\n} and {\n \"NOT\": "RECOMMENDED"\n}",
"hostname": "server",
"pid": 3804,
"diff": 0
} */
// ----
// objects get merged/ assigned - string overwrites msg
log.debug({ object: 1 }, { json: true }, [1, 2, 3], '%s #%d', 'message', 1)
/* > {
"level": "DEBUG",
"name": "package",
"msg": "message #1",
"object": 1,
"json": true,
"arr": [1, 2, 3],
"hostname": "server",
"pid": 3804,
"diff": 0
} */
// ----
const err = new Error('baam')
err.status = 500
// error - string overwrites msg
log.error(err, { object: 1 }, 'A very bad Error')
/* > {
"level": "ERROR",
"name": "package",
"msg": "A very bad Error",
"err": {
"name": "Error",
"stack": "Error: baam\n ...\n at bootstrap_node.js:608:3",
"status": 500
},
"object": 1,
"hostname": "server",
"pid": 3804,
"diff": 20
} */
// ----
// only last arr remains in log
log.debug([1, 2, 3], [4, 5, 6])
/* > {
"level": "DEBUG",
"name": "package",
"arr": [4, 5, 6],
"hostname": "server",
"pid": 3804,
"diff": 3
} */
// additional arguments get added as `args`
log.info('formatter %s', 'message1', 'message2', 'message3')
/* > {
"level": "INFO",
"name": "package",
"msg": "formatter message1",
"args": ["message2", "message3"],
"hostname": "server",
"pid": 3804,
"diff": 0
} */
// ----
// using toJSON custom serializers
function reqToJSON () {
const { ip, method, url, headers } = this
const userAgent = headers ? headers['user-agent'] : undefined
return { ip, method, url, userAgent }
}
const req = { // a client request
method: 'GET',
url: '/path',
ip: '10.10.10.10',
headers: { 'user-agent': 'debug-level/1.0' },
socket: {} // ....
}
req.toJSON = reqToJSON
function resToJSON () {
const { statusCode } = this
return { statusCode }
}
const res = { // a server response
statusCode: 403,
socket: {} // ....
}
res.toJSON = resToJSON
log.warn({ req, res })
/* > {
"level": "WARN",
"name": "package",
"req": {
"ip": "10.10.10.10",
"method": "GET",
"url": "/path",
"userAgent": "debug-level/1.0"
},
"res": {
"statusCode": 403
},
"hostname": "server",
"pid": 3804,
"diff": 1
} */