Name: js-handler/node_modules/restify/lib/bunyan_helper.js 
1:
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
2:
 
3:
var Stream = require('stream').Stream;
4:
var util = require('util');
5:
 
6:
var assert = require('assert-plus');
7:
var bunyan = require('bunyan');
8:
var LRU = require('lru-cache');
9:
var uuid = require('node-uuid');
10:
 
11:
 
12:
 
13:
///--- Globals
14:
 
15:
var sprintf = util.format;
16:
var DEFAULT_REQ_ID = uuid.v4();
17:
var STR_FMT = '[object %s<level=%d, limit=%d, maxRequestIds=%d>]';
18:
 
19:
 
20:
 
21:
///--- Helpers
22:
 
23:
function appendStream(streams, s) {
24:
        assert.arrayOfObject(streams, 'streams');
25:
        assert.object(s, 'stream');
26:
 
27:
        if (s instanceof Stream) {
28:
                streams.push({
29:
                        raw: false,
30:
                        stream: s
31:
                });
32:
        } else {
33:
                assert.optionalBool(s.raw, 'stream.raw');
34:
                assert.object(s.stream, 'stream.stream');
35:
                streams.push(s);
36:
        }
37:
}
38:
 
39:
 
40:
///--- API
41:
 
42:
function RequestCaptureStream(opts) {
43:
        assert.object(opts, 'options');
44:
        assert.optionalObject(opts.stream, 'options.stream');
45:
        assert.optionalArrayOfObject(opts.streams, 'options.streams');
46:
        assert.optionalNumber(opts.level, 'options.level');
47:
        assert.optionalNumber(opts.maxRecords, 'options.maxRecords');
48:
        assert.optionalNumber(opts.maxRequestIds, 'options.maxRequestIds');
49:
 
50:
        var self = this;
51:
        Stream.call(this);
52:
 
53:
        this.level = opts.level || bunyan.WARN;
54:
        this.limit = opts.maxRecords || 100;
55:
        this.maxRequestIds = opts.maxRequestIds || 1000;
56:
        this.requestMap = LRU({
57:
                max: self.maxRequestIds
58:
        });
59:
 
60:
 
61:
        this._offset = -1;
62:
        this._rings = [];
63:
 
64:
        this.streams = [];
65:
 
66:
        if (opts.stream)
67:
                appendStream(this.streams, opts.stream);
68:
 
69:
        if (opts.streams)
70:
                opts.streams.forEach(appendStream.bind(null, this.streams));
71:
}
72:
util.inherits(RequestCaptureStream, Stream);
73:
 
74:
 
75:
RequestCaptureStream.prototype.write = function write(record) {
76:
        var req_id = record.req_id || DEFAULT_REQ_ID;
77:
        var ring;
78:
        var self = this;
79:
 
80:
        if (!(ring = this.requestMap.get(req_id))) {
81:
                if (++this._offset > this.maxRequestIds)
82:
                        this._offset = 0;
83:
 
84:
                if (this._rings.length <= this._offset) {
85:
                        this._rings.push(new bunyan.RingBuffer({
86:
                                limit: self.limit
87:
                        }));
88:
                }
89:
 
90:
                ring = this._rings[this._offset];
91:
                ring.records.length = 0;
92:
                this.requestMap.set(req_id, ring);
93:
        }
94:
 
95:
        assert.ok(ring, 'no ring found');
96:
 
97:
        if (record.level >= this.level) {
98:
                ring.records.forEach(function (r) {
99:
                        var ser = JSON.stringify(r, bunyan.safeCycles()) + '\n';
100:
                        self.streams.forEach(function (s) {
101:
                                s.stream.write(s.raw ? r : ser);
102:
                        });
103:
                });
104:
        } else {
105:
                ring.write(record);
106:
        }
107:
};
108:
 
109:
 
110:
RequestCaptureStream.prototype.toString = function toString() {
111:
        return (sprintf(STR_FMT,
112:
                        this.constructor.name,
113:
                        this.level,
114:
                        this.limit,
115:
                        this.maxRequestIds));
116:
};
117:
 
118:
 
119:
 
120:
///--- Serializers
121:
 
122:
function clientReq(req) {
123:
        if (!req)
124:
                return (req);
125:
 
126:
        var host;
127:
 
128:
        try {
129:
                host = req.host.split(':')[0];
130:
        } catch (e) {
131:
                host = false;
132:
        }
133:
 
134:
        return ({
135:
                method: req ? req.method : false,
136:
                url: req ? req.path : false,
137:
                address: host,
138:
                port: req ? req.port : false,
139:
                headers: req ? req.headers : false
140:
        });
141:
}
142:
 
143:
 
144:
function clientRes(res) {
145:
        if (!res || !res.statusCode)
146:
                return (res);
147:
 
148:
        return ({
149:
                statusCode: res.statusCode,
150:
                headers: res.headers
151:
        });
152:
}
153:
 
154:
 
155:
var SERIALIZERS = {
156:
        err: bunyan.stdSerializers.err,
157:
        req: bunyan.stdSerializers.req,
158:
        res: bunyan.stdSerializers.res,
159:
        client_req: clientReq,
160:
        client_res: clientRes
161:
};
162:
 
163:
 
164:
///--- Exports
165:
 
166:
module.exports = {
167:
        RequestCaptureStream: RequestCaptureStream,
168:
        serializers: SERIALIZERS,
169:
 
170:
        createLogger: function createLogger(name) {
171:
                return (bunyan.createLogger({
172:
                        name: name,
173:
                        serializers: SERIALIZERS,
174:
                        streams: [ {
175:
                                level: 'warn',
176:
                                stream: process.stderr
177:
                        }, {
178:
                                level: 'debug',
179:
                                type: 'raw',
180:
                                stream: new RequestCaptureStream({
181:
                                        stream: process.stderr
182:
                                })
183:
                        } ]
184:
                }));
185:
        }
186:
};