« index
Coverage for /Users/yunong/workspace/node-restify/lib/bunyan_helper.js : 78%
233 lines |
183 run |
50 missing |
2 partial |
29 blocks |
7 blocks run |
22 blocks missing
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 | // Copyright 2012 Mark Cavage, Inc. All rights reserved. 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('node-uuid'); ///--- Globals var sprintf = util.format; var DEFAULT_REQ_ID = uuid.v4(); var STR_FMT = '[object %s<level=%d, limit=%d, maxRequestIds=%d>]'; ///--- Helpers 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. * * @param {Object} options contains the parameters: * - {Object} stream The stream to which to write when dumping captured * records. One of `stream` or `streams` must be specified. * - {Array} streams One of `stream` or `streams` must be specified. * - {Number|String} level The level at which to trigger dumping captured * records. Defaults to bunyan.WARN. * - {Number} maxRecords Number of records to capture. Default 100. * - {Number} maxRequestIds Number of simultaneous request id capturing * buckets to maintain. Default 1000. * - {Boolean} dumpDefault If true, then dump captured records on the * *default* request id when dumping. I.e. dump records logged without * a "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; this.requestMap = 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); 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'); 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 (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 (s) { s.stream.write(s.raw ? r : ser); }); } defaultRing.records.length = 0; } } else { ring.write(record); } }; RequestCaptureStream.prototype.toString = function toString() { return (sprintf(STR_FMT, this.constructor.name, this.level, this.limit, this.maxRequestIds)); }; ///--- Serializers 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 }); } function clientRes(res) { if (!res || !res.statusCode) return (res); return ({ statusCode: res.statusCode, headers: res.headers }); } var SERIALIZERS = { err: bunyan.stdSerializers.err, req: bunyan.stdSerializers.req, res: bunyan.stdSerializers.res, client_req: clientReq, client_res: clientRes }; ///--- Exports module.exports = { RequestCaptureStream: RequestCaptureStream, serializers: SERIALIZERS, createLogger: 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 }) } ] })); } }; |