Merge pull request #54 from arifamirani/master
Add support for GELF logging using UDP
This commit is contained in:
@@ -220,6 +220,24 @@ This was mainly created for [cluster](https://github.com/LearnBoost/cluster), bu
|
|||||||
.listen(3000);
|
.listen(3000);
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
## gelf logger
|
||||||
|
|
||||||
|
A gelf logger has been added to log4js, by [arifamirani](https://github.com/arifamirani). This allows log4js to log to [GELF](http://www.graylog2.org/about/gelf) compatible servers such as [Graylog](http://www.graylog2.org/). This is currently configuration based and needs the following configuration to be added to log4j.json. For example:
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
{
|
||||||
|
"appenders": [
|
||||||
|
{
|
||||||
|
"type": "gelf",
|
||||||
|
"host": "logs.mydomain.com", //defaults to localhost
|
||||||
|
"hostname":"mrs-dev", //defaults to the value returned by os.hostname()
|
||||||
|
"port": "12201", //defaults to 12201
|
||||||
|
"facility": "myapp" //defaults to nodejs-server
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
## author (of this node version)
|
## author (of this node version)
|
||||||
|
|
||||||
Gareth Jones (csausdev - gareth.jones@sensis.com.au)
|
Gareth Jones (csausdev - gareth.jones@sensis.com.au)
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
var compress = require('compress-buffer').compress;
|
||||||
|
var layouts = require('../layouts');
|
||||||
|
var levels = require('../levels');
|
||||||
|
var dgram = require('dgram');
|
||||||
|
var util = require('util');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
*/
|
||||||
|
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');
|
||||||
|
|
||||||
|
var client = dgram.createSocket("udp4");
|
||||||
|
|
||||||
|
process.on('exit', function() {
|
||||||
|
if (client) client.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
function preparePacket(loggingEvent) {
|
||||||
|
var msg = {};
|
||||||
|
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 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);
|
||||||
|
} catch(e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
return function(loggingEvent) {
|
||||||
|
logEventBuffer.push(loggingEvent);
|
||||||
|
flushBuffer();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function configure(config) {
|
||||||
|
var layout;
|
||||||
|
if (config.layout) {
|
||||||
|
layout = layouts.layout(config.layout.type, config.layout);
|
||||||
|
}
|
||||||
|
return gelfAppender(layout, config.host, config.port, config.hostname, config.facility);
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.name = "gelf";
|
||||||
|
exports.appender = gelfAppender;
|
||||||
|
exports.configure = configure;
|
||||||
+2
-1
@@ -22,7 +22,8 @@
|
|||||||
"lib": "lib"
|
"lib": "lib"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "0.1.15"
|
"async": "0.1.15",
|
||||||
|
"compress-buffer": ">= 0.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"vows": ">=0.5.2",
|
"vows": ">=0.5.2",
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
var vows = require('vows')
|
||||||
|
, log4js = require('../lib/log4js')
|
||||||
|
, assert = require('assert')
|
||||||
|
, util = require('util')
|
||||||
|
, dgram = require("dgram");
|
||||||
|
|
||||||
|
log4js.configure({ "appenders": [{"type": "gelf"}] }, undefined);
|
||||||
|
|
||||||
|
vows.describe('log4js gelfAppender').addBatch({
|
||||||
|
|
||||||
|
'with default gelfAppender settings': {
|
||||||
|
topic: function() {
|
||||||
|
var logger = log4js.getLogger();
|
||||||
|
|
||||||
|
//Start local dgram server to act as GELF server
|
||||||
|
var server = dgram.createSocket("udp4");
|
||||||
|
//Assert as soon as message arrives
|
||||||
|
server.on("message", this.callback);
|
||||||
|
//Send a fake message as soon as server is ready
|
||||||
|
server.on("listening", function () {
|
||||||
|
logger.info("This should be a packet of size 161 bytes at the server");
|
||||||
|
});
|
||||||
|
|
||||||
|
//Listen on default values
|
||||||
|
server.bind(12201, 'localhost');
|
||||||
|
},
|
||||||
|
'should receive log messages at the local gelf server': function(err, packet) {
|
||||||
|
assert.ok(packet.size > 0, "Recevied blank message");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).export(module);
|
||||||
Reference in New Issue
Block a user