wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
 
'use strict';
 
var Stream = require('stream').Stream;
var util = require('util');
 
var assert = require('assert-plus');
var bunyan = require('bunyan');
var LRU = require('lru-cache');
var uuid = require('uuid');
 
///--- Globals
 
var sprintf = util.format;
var DEFAULT_REQ_ID = uuid.v4();
var STR_FMT = '[object %s<level=%d, limit=%d, maxRequestIds=%d>]';
 
///--- Helpers
 
/**
 * Appends streams
 *
 * @private
 * @function appendStream
 * @param    {Stream}    streams - the stream to append to
 * @param    {Stream}    s       - the stream to append
 * @returns  {undefined} no return value
 */
function appendStream(streams, s) {
    assert.arrayOfObject(streams, 'streams');
    assert.object(s, 'stream');
 
    if (s instanceof Stream) {
        streams.push({
            raw: false,
            stream: s
        });
    } else {
        assert.optionalBool(s.raw, 'stream.raw');
        assert.object(s.stream, 'stream.stream');
        streams.push(s);
    }
}
 
///--- API
/**
 * A Bunyan stream to capture records in a ring buffer and only pass through
 * on a higher-level record. E.g. buffer up all records but only dump when
 * getting a WARN or above.
 *
 * @public
 * @class
 * @param {Object} opts -                contains the parameters:
 * @param {Object} opts.stream -         The stream to which to write when
 *                                       dumping captured records.
 *                                       One of `stream`
 *                                       or `streams` must be specified.
 * @param {Array} opts.streams -         One of `stream` or `streams` must be
 *                                       specified.
 * @param {Number | String} opts.level - The level at which to trigger dumping
 *                                       captured records. Defaults to
 *                                       bunyan.WARN.
 * @param {Number} opts.maxRecords -     Number of records to capture. Default
 *                                       100.
 * @param {Number} opts.maxRequestIds -  Number of simultaneous request id
 *                                       capturing buckets to maintain. Default
 *                                       1000.
 * @param {Boolean} opts.dumpDefault -   If true, then dump captured records on
 *                                       the *default* request id when dumping.
 *                                       I.e. dump records logged without
 *                                       "req_id" field. Default false.
 */
function RequestCaptureStream(opts) {
    assert.object(opts, 'options');
    assert.optionalObject(opts.stream, 'options.stream');
    assert.optionalArrayOfObject(opts.streams, 'options.streams');
    assert.optionalNumber(opts.level, 'options.level');
    assert.optionalNumber(opts.maxRecords, 'options.maxRecords');
    assert.optionalNumber(opts.maxRequestIds, 'options.maxRequestIds');
    assert.optionalBool(opts.dumpDefault, 'options.dumpDefault');
 
    var self = this;
    Stream.call(this);
 
    this.level = opts.level ? bunyan.resolveLevel(opts.level) : bunyan.WARN;
    this.limit = opts.maxRecords || 100;
    this.maxRequestIds = opts.maxRequestIds || 1000;
    // eslint-disable-next-line new-cap
    // need to initialize using `new`
    this.requestMap = new LRU({
        max: self.maxRequestIds
    });
    this.dumpDefault = opts.dumpDefault;
 
    this._offset = -1;
    this._rings = [];
 
    this.streams = [];
 
    if (opts.stream) {
        appendStream(this.streams, opts.stream);
    }
 
    if (opts.streams) {
        opts.streams.forEach(appendStream.bind(null, this.streams));
    }
 
    this.haveNonRawStreams = false;
 
    for (var i = 0; i < this.streams.length; i++) {
        if (!this.streams[i].raw) {
            this.haveNonRawStreams = true;
            break;
        }
    }
}
util.inherits(RequestCaptureStream, Stream);
 
/**
 * Write to the stream
 *
 * @public
 * @function write
 * @param    {Object}    record - a bunyan log record
 * @returns  {undefined} no return value
 */
RequestCaptureStream.prototype.write = function write(record) {
    var req_id = record.req_id || DEFAULT_REQ_ID;
    var ring;
    var self = this;
 
    if (!(ring = this.requestMap.get(req_id))) {
        if (++this._offset > this.maxRequestIds) {
            this._offset = 0;
        }
 
        if (this._rings.length <= this._offset) {
            this._rings.push(
                new bunyan.RingBuffer({
                    limit: self.limit
                })
            );
        }
 
        ring = this._rings[this._offset];
        ring.records.length = 0;
        this.requestMap.set(req_id, ring);
    }
 
    assert.ok(ring, 'no ring found');
 
    // write the record to the ring.
    ring.write(record);
    // triger dumping of all the records
    if (record.level >= this.level) {
        var i, r, ser;
 
        for (i = 0; i < ring.records.length; i++) {
            r = ring.records[i];
 
            if (this.haveNonRawStreams) {
                ser = JSON.stringify(r, bunyan.safeCycles()) + '\n';
            }
            self.streams.forEach(function forEach(s) {
                s.stream.write(s.raw ? r : ser);
            });
        }
        ring.records.length = 0;
 
        if (this.dumpDefault) {
            var defaultRing = self.requestMap.get(DEFAULT_REQ_ID);
 
            for (i = 0; i < defaultRing.records.length; i++) {
                r = defaultRing.records[i];
 
                if (this.haveNonRawStreams) {
                    ser = JSON.stringify(r, bunyan.safeCycles()) + '\n';
                }
                self.streams.forEach(function forEach(s) {
                    s.stream.write(s.raw ? r : ser);
                });
            }
            defaultRing.records.length = 0;
        }
    }
};
 
/**
 * toString() serialization
 *
 * @public
 * @function toString
 * @returns  {String} stringified instance
 */
RequestCaptureStream.prototype.toString = function toString() {
    return sprintf(
        STR_FMT,
        this.constructor.name,
        this.level,
        this.limit,
        this.maxRequestIds
    );
};
 
///--- Serializers
 
var SERIALIZERS = {
    err: bunyan.stdSerializers.err,
    req: bunyan.stdSerializers.req,
    res: bunyan.stdSerializers.res,
    client_req: clientReq,
    client_res: clientRes
};
 
/**
 * A request serializer. returns a stripped down object for logging.
 *
 * @private
 * @function clientReq
 * @param    {Object} req - the request object
 * @returns  {Object} serialized req
 */
function clientReq(req) {
    if (!req) {
        return req;
    }
 
    var host;
 
    try {
        host = req.host.split(':')[0];
    } catch (e) {
        host = false;
    }
 
    return {
        method: req ? req.method : false,
        url: req ? req.path : false,
        address: host,
        port: req ? req.port : false,
        headers: req ? req.headers : false
    };
}
 
/**
 * A response serializer. returns a stripped down object for logging.
 *
 * @private
 * @function clientRes
 * @param    {Object} res - the response object
 * @returns  {Object} serialized response
 */
function clientRes(res) {
    if (!res || !res.statusCode) {
        return res;
    }
 
    return {
        statusCode: res.statusCode,
        headers: res.headers
    };
}
 
/**
 * Create a bunyan logger.
 *
 * @public
 * @function createLogger
 * @param    {String}     name - of the logger
 * @returns  {Object}          bunyan logger
 */
function createLogger(name) {
    return bunyan.createLogger({
        name: name,
        serializers: SERIALIZERS,
        streams: [
            {
                level: 'warn',
                stream: process.stderr
            },
            {
                level: 'debug',
                type: 'raw',
                stream: new RequestCaptureStream({
                    stream: process.stderr
                })
            }
        ]
    });
}
 
///--- Exports
 
module.exports = {
    RequestCaptureStream: RequestCaptureStream,
    serializers: SERIALIZERS,
    createLogger: createLogger
};