-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
log_format.js
171 lines (156 loc) · 5.21 KB
/
log_format.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import Stream from 'stream';
import moment from 'moment-timezone';
import { get, _ } from 'lodash';
import numeral from '@elastic/numeral';
import chalk from 'chalk';
import stringify from 'json-stringify-safe';
import querystring from 'querystring';
import applyFiltersToKeys from './apply_filters_to_keys';
import { inspect } from 'util';
function serializeError(err = {}) {
return {
message: err.message,
name: err.name,
stack: err.stack,
code: err.code,
signal: err.signal
};
}
const levelColor = function (code) {
if (code < 299) return chalk.green(code);
if (code < 399) return chalk.yellow(code);
if (code < 499) return chalk.magentaBright(code);
return chalk.red(code);
};
export default class TransformObjStream extends Stream.Transform {
constructor(config) {
super({
readableObjectMode: false,
writableObjectMode: true
});
this.config = config;
}
filter(data) {
if (!this.config.filter) return data;
return applyFiltersToKeys(data, this.config.filter);
}
_transform(event, enc, next) {
const data = this.filter(this.readEvent(event));
this.push(this.format(data) + '\n');
next();
}
extractAndFormatTimestamp(data, format) {
const { timezone } = this.config;
const date = moment(data['@timestamp']);
if (timezone) {
date.tz(timezone);
}
return date.format(format);
}
readEvent(event) {
const data = {
type: event.event,
'@timestamp': event.timestamp,
tags: [].concat(event.tags || []),
pid: event.pid
};
if (data.type === 'response') {
_.defaults(data, _.pick(event, [
'method',
'statusCode'
]));
const source = get(event, 'source', {});
data.req = {
url: event.path,
method: event.method || '',
headers: event.headers,
remoteAddress: source.remoteAddress,
userAgent: source.remoteAddress,
referer: source.referer
};
let contentLength = 0;
if (typeof event.responsePayload === 'object') {
contentLength = stringify(event.responsePayload).length;
} else {
contentLength = String(event.responsePayload).length;
}
data.res = {
statusCode: event.statusCode,
responseTime: event.responseTime,
contentLength: contentLength
};
const query = querystring.stringify(event.query);
if (query) data.req.url += '?' + query;
data.message = data.req.method.toUpperCase() + ' ';
data.message += data.req.url;
data.message += ' ';
data.message += levelColor(data.res.statusCode);
data.message += ' ';
data.message += chalk.gray(data.res.responseTime + 'ms');
data.message += chalk.gray(' - ' + numeral(contentLength).format('0.0b'));
}
else if (data.type === 'ops') {
_.defaults(data, _.pick(event, [
'pid',
'os',
'proc',
'load'
]));
data.message = chalk.gray('memory: ');
data.message += numeral(get(data, 'proc.mem.heapUsed')).format('0.0b');
data.message += ' ';
data.message += chalk.gray('uptime: ');
data.message += numeral(get(data, 'proc.uptime')).format('00:00:00');
data.message += ' ';
data.message += chalk.gray('load: [');
data.message += get(data, 'os.load', []).map(function (val) {
return numeral(val).format('0.00');
}).join(' ');
data.message += chalk.gray(']');
data.message += ' ';
data.message += chalk.gray('delay: ');
data.message += numeral(get(data, 'proc.delay')).format('0.000');
}
else if (data.type === 'error') {
data.level = 'error';
data.error = serializeError(event.error);
data.url = event.url;
const message = get(event, 'error.message');
data.message = message || 'Unknown error (no message)';
}
else if (event.data instanceof Error) {
data.type = 'error';
data.level = _.contains(event.tags, 'fatal') ? 'fatal' : 'error';
data.error = serializeError(event.data);
const message = get(event, 'data.message');
data.message = message || 'Unknown error object (no message)';
}
else if (_.isPlainObject(event.data) && event.data.tmpl) {
_.assign(data, event.data);
data.tmpl = undefined;
data.message = _.template(event.data.tmpl)(event.data);
}
else {
data.message = _.isString(event.data) ? event.data : inspect(event.data);
}
return data;
}
}