2013-06-04 06:17:36 +08:00
|
|
|
"use strict";
|
2012-05-29 14:49:12 +08:00
|
|
|
var zlib = require('zlib');
|
2012-01-11 18:42:24 +08:00
|
|
|
var layouts = require('../layouts');
|
|
|
|
var levels = require('../levels');
|
|
|
|
var dgram = require('dgram');
|
|
|
|
var util = require('util');
|
2013-06-24 06:51:10 +08:00
|
|
|
var debug = require('../debug')('GELF Appender');
|
2012-01-11 18:42:24 +08:00
|
|
|
|
2012-02-13 05:54:35 +08:00
|
|
|
var LOG_EMERG=0; // system is unusable
|
|
|
|
var LOG_ALERT=1; // action must be taken immediately
|
|
|
|
var LOG_CRIT=2; // critical conditions
|
|
|
|
var LOG_ERR=3; // error conditions
|
|
|
|
var LOG_ERROR=3; // because people WILL typo
|
|
|
|
var LOG_WARNING=4; // warning conditions
|
|
|
|
var LOG_NOTICE=5; // normal, but significant, condition
|
|
|
|
var LOG_INFO=6; // informational message
|
|
|
|
var LOG_DEBUG=7; // debug-level message
|
|
|
|
|
|
|
|
var levelMapping = {};
|
|
|
|
levelMapping[levels.ALL] = LOG_DEBUG;
|
|
|
|
levelMapping[levels.TRACE] = LOG_DEBUG;
|
|
|
|
levelMapping[levels.DEBUG] = LOG_DEBUG;
|
|
|
|
levelMapping[levels.INFO] = LOG_INFO;
|
|
|
|
levelMapping[levels.WARN] = LOG_WARNING;
|
|
|
|
levelMapping[levels.ERROR] = LOG_ERR;
|
|
|
|
levelMapping[levels.FATAL] = LOG_CRIT;
|
|
|
|
|
2012-01-11 18:42:24 +08:00
|
|
|
/**
|
|
|
|
* GELF appender that supports sending UDP packets to a GELF compatible server such as Graylog
|
|
|
|
*
|
|
|
|
* @param layout a function that takes a logevent and returns a string (defaults to none).
|
|
|
|
* @param host - host to which to send logs (default:localhost)
|
|
|
|
* @param port - port at which to send logs to (default:12201)
|
|
|
|
* @param hostname - hostname of the current host (default:os hostname)
|
|
|
|
* @param facility - facility to log to (default:nodejs-server)
|
|
|
|
*/
|
2012-02-10 15:14:50 +08:00
|
|
|
function gelfAppender (layout, host, port, hostname, facility) {
|
2013-07-08 13:24:29 +08:00
|
|
|
var config, customFields;
|
|
|
|
if (typeof(host) === 'object') {
|
|
|
|
config = host;
|
|
|
|
host = config.host;
|
|
|
|
port = config.port;
|
|
|
|
hostname = config.hostname;
|
|
|
|
facility = config.facility;
|
|
|
|
customFields = config.customFields;
|
|
|
|
}
|
2013-06-04 06:17:36 +08:00
|
|
|
|
|
|
|
host = host || 'localhost';
|
|
|
|
port = port || 12201;
|
|
|
|
hostname = hostname || require('os').hostname();
|
|
|
|
facility = facility || 'nodejs-server';
|
|
|
|
layout = layout || layouts.messagePassThroughLayout;
|
2013-07-05 11:23:59 +08:00
|
|
|
|
|
|
|
var defaultCustomFields = customFields || {};
|
2013-06-04 06:17:36 +08:00
|
|
|
|
|
|
|
var client = dgram.createSocket("udp4");
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
|
|
|
if (client) client.close();
|
|
|
|
});
|
2013-07-05 10:54:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add custom fields (start with underscore )
|
2013-07-08 13:24:29 +08:00
|
|
|
* - if the first object passed to the logger contains 'GELF' field,
|
|
|
|
* copy the underscore fields to the message
|
2013-07-05 10:54:31 +08:00
|
|
|
* @param loggingEvent
|
|
|
|
* @param msg
|
|
|
|
*/
|
|
|
|
function addCustomFields(loggingEvent, msg){
|
2013-07-05 11:23:59 +08:00
|
|
|
|
2013-07-08 13:24:29 +08:00
|
|
|
/* append defaultCustomFields firsts */
|
|
|
|
Object.keys(defaultCustomFields).forEach(function(key) {
|
|
|
|
// skip _id field for graylog2, skip keys not starts with UNDERSCORE
|
|
|
|
if (key.match(/^_/) && key !== "_id") {
|
|
|
|
msg[key] = defaultCustomFields[key];
|
2013-07-05 11:23:59 +08:00
|
|
|
}
|
2013-07-08 13:24:29 +08:00
|
|
|
});
|
2013-07-05 11:23:59 +08:00
|
|
|
|
2013-07-08 13:24:29 +08:00
|
|
|
/* append custom fields per message */
|
|
|
|
var data = loggingEvent.data;
|
|
|
|
if (!Array.isArray(data) || data.length === 0) return;
|
|
|
|
var firstData = data[0];
|
|
|
|
|
|
|
|
if (!firstData.GELF) return; // identify with GELF field defined
|
|
|
|
Object.keys(firstData).forEach(function(key) {
|
|
|
|
// skip _id field for graylog2, skip keys not starts with UNDERSCORE
|
|
|
|
if (key.match(/^_/) || key !== "_id") {
|
|
|
|
msg[key] = firstData[key];
|
2013-07-05 10:54:31 +08:00
|
|
|
}
|
2013-07-08 13:24:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
/* the custom field object should be removed, so it will not be looged by the later appenders */
|
|
|
|
loggingEvent.data.shift();
|
2013-07-05 10:54:31 +08:00
|
|
|
}
|
|
|
|
|
2013-06-04 06:17:36 +08:00
|
|
|
function preparePacket(loggingEvent) {
|
|
|
|
var msg = {};
|
2013-07-05 10:54:31 +08:00
|
|
|
addCustomFields(loggingEvent, msg);
|
2013-06-04 06:17:36 +08:00
|
|
|
msg.full_message = layout(loggingEvent);
|
|
|
|
msg.short_message = msg.full_message;
|
|
|
|
|
|
|
|
msg.version="1.0";
|
|
|
|
msg.timestamp = msg.timestamp || new Date().getTime() / 1000 >> 0;
|
|
|
|
msg.host = hostname;
|
|
|
|
msg.level = levelMapping[loggingEvent.level || levels.DEBUG];
|
|
|
|
msg.facility = facility;
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendPacket(packet) {
|
|
|
|
try {
|
|
|
|
client.send(packet, 0, packet.length, port, host);
|
|
|
|
} catch(e) {}
|
|
|
|
}
|
2012-01-11 18:42:24 +08:00
|
|
|
|
2013-06-04 06:17:36 +08:00
|
|
|
return function(loggingEvent) {
|
|
|
|
var message = preparePacket(loggingEvent);
|
|
|
|
zlib.gzip(new Buffer(JSON.stringify(message)), function(err, packet) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err.stack);
|
|
|
|
} else {
|
|
|
|
if (packet.length > 8192) {
|
|
|
|
debug("Message packet length (" + packet.length + ") is larger than 8k. Not sending");
|
|
|
|
} else {
|
|
|
|
sendPacket(packet);
|
|
|
|
}
|
|
|
|
}
|
2012-01-11 18:42:24 +08:00
|
|
|
});
|
2013-06-04 06:17:36 +08:00
|
|
|
};
|
2012-01-11 18:42:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function configure(config) {
|
2013-06-04 06:17:36 +08:00
|
|
|
var layout;
|
|
|
|
if (config.layout) {
|
|
|
|
layout = layouts.layout(config.layout.type, config.layout);
|
|
|
|
}
|
2013-07-08 13:24:29 +08:00
|
|
|
return gelfAppender(layout, config);
|
2012-01-11 18:42:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.appender = gelfAppender;
|
|
|
|
exports.configure = configure;
|