bunyan-lumberjack
Version:
Send bunyan logs to logstash using the lumberjack protocol.
152 lines (136 loc) • 4.63 kB
JavaScript
// Generated by CoffeeScript 1.7.1
(function() {
var BunyanLumberjackStream, LEVELS, Writable, bunyan, clone, lumberjack, merge_options,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Writable = require('stream').Writable;
lumberjack = require('lumberjack-protocol');
bunyan = require('bunyan');
LEVELS = (function() {
var answer;
answer = {};
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(function(level) {
return answer[bunyan[level.toUpperCase()]] = level;
});
return answer;
})();
clone = function(obj) {
var answer, key, value;
answer = {};
for (key in obj) {
value = obj[key];
answer[key] = value;
}
return answer;
};
/*
Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
@param obj1
@param obj2
@returns obj3 a new object based on obj1 and obj2
*/
merge_options = function(obj1, obj2) {
var attrname, obj3;
obj3 = {};
for (attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (attrname in obj2) {
obj3[attrname] = obj2[attrname];
}
return obj3;
};
BunyanLumberjackStream = (function(_super) {
__extends(BunyanLumberjackStream, _super);
function BunyanLumberjackStream(tlsOptions, lumberjackOptions, options) {
var _ref, _ref1, _ref2, _ref3;
if (lumberjackOptions == null) {
lumberjackOptions = {};
}
if (options == null) {
options = {};
}
BunyanLumberjackStream.__super__.constructor.call(this, {
objectMode: true
});
this._client = lumberjack.client(tlsOptions, lumberjackOptions);
this._client.on('connect', (function(_this) {
return function(count) {
return _this.emit('connect', count);
};
})(this));
this._client.on('dropped', (function(_this) {
return function(count) {
return _this.emit('dropped', count);
};
})(this));
this._client.on('disconnect', (function(_this) {
return function(err) {
return _this.emit('disconnect', err);
};
})(this));
this._host = require('os').hostname();
this._tags = (_ref = options.tags) != null ? _ref : ['bunyan'];
this._type = (_ref1 = options.type) != null ? _ref1 : 'json';
this._application = (_ref2 = options.appName) != null ? _ref2 : process.title;
this._metadata = (_ref3 = options.metadata) != null ? _ref3 : {};
this.on('finish', (function(_this) {
return function() {
return _this._client.close();
};
})(this));
}
BunyanLumberjackStream.prototype._write = function(entry, encoding, done) {
var bunyanLevel, dataFrame, host, _ref, _ref1;
entry = clone(entry);
host = (_ref = entry.hostname) != null ? _ref : this._host;
bunyanLevel = entry.level;
if (LEVELS[entry.level] != null) {
entry.level = LEVELS[entry.level];
}
entry.message = (_ref1 = entry.msg) != null ? _ref1 : '';
delete entry.msg;
if (entry.time != null) {
entry['@timestamp'] = entry.time.toISOString();
delete entry.time;
}
delete entry.v;
if (entry.tags == null) {
entry.tags = this._tags;
}
if (entry._md) {
entry["@metadata"] = entry._md;
delete entry._md;
}
if (entry["@metadata"] == null) {
entry["@metadata"] = this._metadata;
} else {
entry["@metadata"] = merge_options(this._metadata, entry["@metadata"]);
}
entry.source = "" + host + "/" + this._application;
dataFrame = {
line: JSON.stringify(entry),
host: host,
bunyanLevel: bunyanLevel
};
if (this._type != null) {
dataFrame.type = this._type;
}
this._client.writeDataFrame(dataFrame);
return done();
};
return BunyanLumberjackStream;
})(Writable);
module.exports = function(options) {
if (options == null) {
options = {};
}
if (options.lumberjackOptions == null) {
options.lumberjackOptions = {};
}
if (options.lumberjackOptions.unref == null) {
options.lumberjackOptions.unref = true;
}
return new BunyanLumberjackStream(options.tlsOptions, options.lumberjackOptions, options);
};
}).call(this);