added more gelf tests

This commit is contained in:
Gareth Jones
2012-02-13 08:54:35 +11:00
parent 0a422e5749
commit cff20b99e3
2 changed files with 148 additions and 76 deletions

View File

@@ -4,6 +4,25 @@ var levels = require('../levels');
var dgram = require('dgram');
var util = require('util');
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;
/**
* GELF appender that supports sending UDP packets to a GELF compatible server such as Graylog
*
@@ -15,32 +34,11 @@ var util = require('util');
*/
function gelfAppender (layout, host, port, hostname, facility) {
var logEventBuffer = [];
LOG_EMERG=0; // system is unusable
LOG_ALERT=1; // action must be taken immediately
LOG_CRIT=2; // critical conditions
LOG_ERR=3; // error conditions
LOG_ERROR=3; // because people WILL typo
LOG_WARNING=4; // warning conditions
LOG_NOTICE=5; // normal, but significant, condition
LOG_INFO=6; // informational message
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;
host = host || 'localhost';
port = port || 12201;
hostname = hostname || require('os').hostname();
facility = facility || 'nodejs-server';
layout = layout || layouts.patternLayout('%m');
layout = layout || layouts.messagePassThroughLayout;
var client = dgram.createSocket("udp4");
@@ -61,18 +59,6 @@ function gelfAppender (layout, host, port, hostname, facility) {
return msg;
}
function flushBuffer() {
while (logEventBuffer.length > 0) {
var message = preparePacket(logEventBuffer.shift());
var packet = compress(new Buffer(JSON.stringify(message)));
if (packet.length > 8192) {
util.debug("Message packet length (" + packet.length + ") is larger than 8k. Not sending");
} else {
sendPacket(packet);
}
}
}
function sendPacket(packet) {
try {
client.send(packet, 0, packet.length, port, host);
@@ -80,8 +66,13 @@ function gelfAppender (layout, host, port, hostname, facility) {
}
return function(loggingEvent) {
logEventBuffer.push(loggingEvent);
flushBuffer();
var message = preparePacket(loggingEvent);
var packet = compress(new Buffer(JSON.stringify(message)));
if (packet.length > 8192) {
util.debug("Message packet length (" + packet.length + ") is larger than 8k. Not sending");
} else {
sendPacket(packet);
}
};
}