diff --git a/graylog.js b/graylog.js index e98ae98..250b2bf 100644 --- a/graylog.js +++ b/graylog.js @@ -2,7 +2,8 @@ var zlib = require('zlib'), crypto = require('crypto'), dgram = require('dgram'), util = require('util'), - EventEmitter = require('events').EventEmitter; + EventEmitter = require('events').EventEmitter, + assert = require('assert'); /** * Graylog instances emit errors. That means you really really should listen for them, @@ -18,6 +19,10 @@ var graylog = function graylog(config) { this.client = null; this.hostname = config.hostname || require('os').hostname(); this.facility = config.facility || 'Node.js'; + this.deflate = config.deflate || 'optimal'; + assert( + this.deflate === 'optimal' || this.deflate === 'always' || this.deflate === 'never', + 'deflate must be one of "optimal", "always", or "never". was "' + this.deflate + '"'); this._unsentMessages = 0; this._unsentChunks = 0; @@ -164,7 +169,7 @@ graylog.prototype._log = function log(short_message, full_message, additionalFie // Compression payload = new Buffer(JSON.stringify(message)); - zlib.deflate(payload, function (err, buffer) { + function sendPayload(err, buffer) { if (err) { that._unsentMessages -= 1; return that.emitError(err); @@ -235,7 +240,13 @@ graylog.prototype._log = function log(short_message, full_message, additionalFie send(); }); - }); + } + + if (this.deflate === 'never' || (this.deflate === 'optimal' && payload.length <= this._bufferSize)) { + sendPayload(null, payload); + } else { + zlib.deflate(payload, sendPayload); + } }; graylog.prototype.send = function (chunk, server, cb) { diff --git a/test.js b/test.js index f8cad06..373810e 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,6 @@ var graylog = require('./graylog'), fs = require('fs'), + assert = require('assert'), file, data, servers = [ @@ -49,7 +50,36 @@ client.log('ParametersTest - Short message and full message', 'Full message'); client.log('ParametersTest - Short Message with full message and json', 'Full message', {cool: 'beans'}); console.log(''); +console.log('---------------------------------------------'); +console.log('Sending without deflate'); +console.log('---------------------------------------------'); +client.deflate = 'never'; +for (var i = 4; i <= 64; i *= 2) { + file = './data/' + i + '.dat'; + data = fs.readFileSync(file); + console.log('sending', file); + client.critical('Test 4 ' + file, data.toString(), {datafile: i + '.dat'}); +} +client.deflate = 'optimal'; +console.log(''); + client.close(function () { console.log('Insertion complete. Please check', 'http://' + servers[0].host + ':3000', 'and verify that insertion was successfull'); console.log(''); }); + +console.log('---------------------------------------------'); +console.log('Checking deflate assertion'); +console.log('---------------------------------------------'); +try { + new graylog.graylog({ + servers: servers, + facility: 'Test logger / Node.JS Test Script', + deflate: 'not an option' + }); + throw new Error('should not get here') +} catch (err) { + assert( + err.message === 'deflate must be one of "optimal", "always", or "never". was "not an option"', + 'assertion msg was wrong: ' + err.message); +}