-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (98 loc) · 3.15 KB
/
index.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
const Hoek = require('hoek');
const Stream = require('stream');
const formatters = require('./formatters');
const SafeStringify = require('json-stringify-safe');
const {template, timestamp, font, toUpper, toString, stringify, math, formatter} = formatters;
/**
* Round bytes value to Mb
*
* @param {int} value Value to be processed
* @returns {float}
*/
const bytesToMb = formatter(value => {
const mb = Math.round(value) / (1024 * 1024)
return Math.round(mb * 100) / 100;
});
/**
* Round all the items in the given array
*
* @param {int} value Value to be processed
* @returns {float}
*/
const roundAll = formatter(value => (value || []).map(item => {
return Math.round(item * 100) / 100;
}));
/**
* Add custom theme to font formatter
*/
font.setTheme({
error: ['red', 'bold'],
success: ['green', 'bold'],
warn: ['yellow', 'bold'],
info: ['blue', 'bold']
});
/**
* Returns theme name depending on code
*
* @param {int} statusCode
* @returns {string}
*/
const statusCodeColor = statusCode => {
if (statusCode >= 500) {
return 'error';
}
if (statusCode >= 400) {
return 'warn';
}
if (statusCode >= 300) {
return 'info';
}
return 'success';
};
/**
* Returns theme name depending on HTTP method
*
* @param {String} method
* @returns {String}
*/
const methodColor = method => {
return ({
GET: 'info',
POST: 'success',
PUT: 'success',
DELETE: 'error',
OPTIONS: 'warn'
})[method.toUpperCase()] || 'info';
};
// Event partials
const pid = template`PID:${'pid'}`;
const tags = template`[${toString('tags')}]`;
const method = template`${toUpper('method')}`;
const responseTime = template`(${'responseTime'}ms)`;
const query = template`${stringify('query')}`;
const data = template`${stringify('data')}`;
const error = template`${'error.message'}\n${font.error('error.stack')}}`;
const event = template`${'event'}`;
const memory = template`${bytesToMb('proc.mem.rss')}`;
const general = template` ${font.info(pid)} ${timestamp('timestamp', 'YYMMDDHHmmssSSS')}`;
const internals = {
defaults: {
log: template`${general} ${font.info(tags)} ${'data'} ${error}`,
ops: template`${general} memory: ${memory}Mb, uptime: ${math.round('proc.uptime')}s, load: [${roundAll('os.load')}]`,
error: template`${general} ${font.bold(event)} ${error}`,
request: template`${general} ${font.info(tags)} ${font.bold(event)} ${font(method, methodColor)} ${'path'} ${data}`,
response: template`${general} ${font.info(tags)} ${font.bold(event)} ${font(method, methodColor)} ${'route'} ${query} ${font('statusCode', statusCodeColor)} ${font.warn(responseTime)}`
}
};
class GoodFormat extends Stream.Transform {
constructor(config = {}) {
super({objectMode: true});
this._settings = Hoek.applyToDefaults(internals.defaults, config);
}
_transform(data, enc, next) {
const formatter = this._settings[data.event];
const output = formatter ? formatter(data) : SafeStringify(data);
return next(null, `${output}\n`);
}
}
module.exports = Object.assign(GoodFormat, formatters);