forked from muzzley/good-bunyan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatters.js
132 lines (119 loc) · 4.27 KB
/
formatters.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
'use strict';
const safeStringify = require('fast-safe-stringify');
/**
* Converts provided num in a string representing Mb amount.
* @private
* @param {number} num - Number to convert in Mb
* @return {string} String with number in Mb and "Mb" suffix
*/
const inMb = num => `${Math.round(num / (1024 * 1024))}Mb`;
/**
* Zips data with provided tag.
* @private
* @param {string} tag - Log tag
* @param {object} data - Data to log
* @return {array} Array with data and the provided tag
*/
const withTag = (tag, data) => [data, `[${tag}]`];
/**
* Formats payload of "ops" events.
* @see {@link https://github.com/hapijs/good/blob/master/API.md#ops}
* @public
* @param {object} payload - Event payload
* @return {array} Array with ops data and "[ops]" tag.
* Ops data is an object with this interface:
* {
* memory: '<Number>Mb' where <Number> is 'resident set size' - the amount of the process held in memory
* uptime: '<Number>s' where <Number> is uptime of the running process in seconds
* load: <String> with useful information (requests, concurrents connections, response times, sockets)
* }
*/
const ops = payload => withTag('ops', {
memory: inMb(payload.proc.mem.rss),
uptime: `${payload.proc.uptime}s`,
load: payload.os.load.join(', ')
});
/**
* Formats payload of "response" events.
* @see {@link https://github.com/hapijs/good/blob/master/API.md#requestsent}
* @public
* @param {object} payload - Event payload
* @return {array} Array with response data and "[response]" tag.
* Response data is an object with this interface:
* {
* instance: <String> maps to request.connection.info.uri
* method: <String> method used by the request - Maps to request.method
* path: <String> incoming path requested - maps to request.path
* statusCode: <Number> the status code of the response
* responseTime: `<Number>ms` where <Number> is calculated value of Date.now() - request.info.received
* requestPayload: <String> stringified version of requestPayload (if any)
* responsePayload: <String> stringified version of responsePayload (if any)
* query: <String> stringified version of query object used by request - maps to request.query
* }
*/
const response = payload => {
let reqPayload;
let resPayload;
if (payload.requestPayload) {
if (typeof payload.requestPayload === 'object' || Array.isArray(payload.requestPayload)) {
reqPayload = safeStringify(payload.requestPayload);
}
}
if (payload.responsePayload) {
if (typeof payload.responsePayload === 'object' || Array.isArray(payload.responsePayload)) {
resPayload = safeStringify(payload.responsePayload);
}
}
const res = {
instance: payload.instance,
method: payload.method,
path: payload.path,
statusCode: payload.statusCode,
responseTime: `${payload.responseTime}ms`,
requestPayload: reqPayload,
responsePayload: resPayload,
query: JSON.stringify(payload.query)
};
return withTag('response', {res});
};
/**
* Formats payload of "error" events.
* @see {@link https://github.com/hapijs/good/blob/master/API.md#requesterror}
* @public
* @param {object} payload - Event payload
* @return {array} Array with error data, a "[error]" tag and the error message.
* Error data is an object with this interface:
* {
* err: <Object> the raw error object
* }
*/
const error = payload => withTag('error', {err: payload.error}).concat([payload.error.message]);
/**
* Formats payload of "log" events with simple formatter.
* @see {@link https://github.com/hapijs/good/blob/master/API.md#serverlog}
* @see simple
* @public
* @param {object} payload - Event payload
* @return {array} Array with log data and a "[log]"
*/
const log = payload => {
const message = withTag('log', payload.data);
// Reverse message when payload.data is not an object in order to level out log
return typeof payload.data !== 'object' ? message.reverse() : message;
};
/**
* Formats payload of "request" events with simple formatter.
* @see {@link https://github.com/hapijs/good/blob/master/API.md#requestlog}
* @see simple
* @public
* @param {object} payload - Event payload
* @return {array} Array with request data and a "[request]" tag
*/
const request = payload => withTag('request', {req: payload});
module.exports = {
ops,
response,
error,
log,
request
};