-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
64 lines (53 loc) · 1.71 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
'use strict';
var logentries = require('le_node'),
Stream = require('stream').Stream,
util = require('util'),
LBS;
exports.LogentriesBunyanStream = LogentriesBunyanStream;
exports.createStream = function (config, options) {
return new LogentriesBunyanStream(config, options);
};
/**
* LogentriesBunyanStream
*
* @param {Object} config Logentries config
* @param {String} config.token
* @param {Function} [config.transform] transforms every log
* @param {String} [config.defaultLevel] (defaults to `info`)
*/
function LogentriesBunyanStream(config, options) {
if (!config || !config.token) throw new Error('config.token must be set');
Stream.call(this);
this.transform = options && options.transform;
this.defaultLevel = options && options.defaultLevel || 'info';
this.writable = true;
config.levels = config.levels || ['debug', 'info', 'notice', 'warning', 'err', 'crit', 'alert', 'emerg'];
this._logger = new logentries(config);
}
util.inherits(LogentriesBunyanStream, Stream);
LBS = LogentriesBunyanStream.prototype;
LBS.write = function (rec) {
if (!rec) throw new Error('nothing passed to log');
if (!this.writable) throw new Error('failed to write to a closed stream');
if ('function' === typeof this.transform) rec = this.transform(rec);
this._logger.log(this._resolveLevel(rec.level), rec);
};
LBS.end = function (rec) {
if (arguments.length) this.write(rec);
this.writable = false;
this._logger.end();
};
LBS.destroy = function () {
this.writable = false;
};
LBS._resolveLevel = function (bunyanLevel) {
var levelToName = {
10: 'trace',
20: 'debug',
30: 'info',
40: 'warn',
50: 'error',
60: 'fatal'
};
return levelToName[bunyanLevel] || this.defaultLevel;
};