-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Using node-graylog2 instead of custom protocol implementation * Moving to mocha for tests and implementing the base tests * Updated readme * Updated package.json * Updated .travic.yml and bumped minumum node version to a more modern 0.10
- Loading branch information
Showing
6 changed files
with
167 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- 0.6 | ||
- 0.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,49 @@ | ||
// winston-graylog2.js: Transport for outputting logs over UDP to Graylog2 | ||
'use strict'; | ||
var util = require('util'), | ||
winston = require('winston'), | ||
compress = require('compress-buffer').compress, | ||
dgram = require('dgram'); | ||
|
||
|
||
var Graylog2 = exports.Graylog2 = winston.transports.Graylog2 = function (options) { | ||
this.name = options.name || 'graylog2'; | ||
this.level = options.level || 'info'; | ||
this.silent = options.silent || false; | ||
this.handleExceptions = options.handleExceptions || false; | ||
this.udpClient = dgram.createSocket('udp4'); | ||
this.udpClient.on('error', function (err) { | ||
// Handle any suprise errors | ||
util.error(err); | ||
}); | ||
|
||
this.graylogHost = options.graylogHost || 'localhost'; | ||
this.graylogPort = options.graylogPort || 12201; | ||
this.graylogHostname = options.graylogHostname || require('os').hostname(); | ||
this.graylogFacility = options.graylogFacility || 'nodejs'; | ||
this.graylogSequence = 0; | ||
graylog2 = require('graylog2'); | ||
|
||
function getMessageLevel(winstonLevel) { | ||
var levels = { | ||
emerg: 'emergency', | ||
alert: 'alert', | ||
crit: 'critical', | ||
error: 'error', | ||
warning: 'warning', | ||
notice: 'notice', | ||
info: 'info', | ||
debug: 'debug' | ||
}; | ||
|
||
return levels[winstonLevel] || levels.info; | ||
} | ||
|
||
var Graylog2 = winston.transports.Graylog2 = function(options) { | ||
options = options || {}; | ||
options.graylog = options.graylog || { | ||
servers: [{ | ||
'host': 'localhost', | ||
port: 12201 | ||
}] | ||
}; | ||
|
||
this.name = options.name || 'graylog2'; | ||
this.level = options.level || 'info'; | ||
this.silent = options.silent || false; | ||
this.handleExceptions = options.handleExceptions || false; | ||
|
||
this.graylog2 = new graylog2.graylog(options.graylog); | ||
|
||
this.graylog2.on('error', function(error) { | ||
console.error('Error while trying to write to graylog2:', error); | ||
}); | ||
}; | ||
|
||
util.inherits(Graylog2, winston.Transport); | ||
|
||
var getMessageLevel = function (winstonLevel) { | ||
switch (winstonLevel) { | ||
case 'silly': | ||
case 'debug': return 7 | ||
case 'verbose': | ||
case 'data': | ||
case 'prompt': | ||
case 'input': | ||
case 'info': return 6 | ||
case 'help': | ||
case 'notice': return 5 | ||
case 'warn': | ||
case 'warning': return 4 | ||
case 'error': return 3 | ||
case 'crit': return 2 | ||
case 'alert': return 1 | ||
case 'emerg': return 0 | ||
default: return 6 | ||
} | ||
Graylog2.prototype.log = function(level, msg, meta, callback) { | ||
this.graylog2[getMessageLevel(level)](msg); | ||
callback(null, true); | ||
}; | ||
|
||
Graylog2.prototype.log = function (level, msg, meta, callback) { | ||
var self = this, message = {}, key; | ||
|
||
if (self.silent) { | ||
return callback(null, true); | ||
} | ||
|
||
// Must be in this format: https://github.com/Graylog2/graylog2-docs/wiki/GELF | ||
message.version = "1.0"; | ||
message.timestamp = +new Date()/1000; | ||
message.host = self.graylogHostname; | ||
message.facility = self.graylogFacility; | ||
message.short_message = msg; | ||
message.full_message = meta || {}; | ||
message.level = getMessageLevel(level); | ||
|
||
if (!!meta) { | ||
for (key in meta) { | ||
if (key !== 'id') { | ||
message['_'+key] = meta[key]; | ||
} | ||
} | ||
} | ||
var flatMessage = util.inspect(message,{ | ||
depth: 4, | ||
showHidden: true | ||
}); | ||
var compressedMessage = compress(new Buffer(flatMessage)); | ||
|
||
if (compressedMessage.length > 8192) { | ||
return callback(new Error("Log message size > 8192 bytes not supported."), null); | ||
} | ||
|
||
this.udpClient.send(compressedMessage, 0, compressedMessage.length, self.graylogPort, self.graylogHost, function (err, bytes) { | ||
if (err) { | ||
callback(err, null); | ||
} else { | ||
callback(null, true); | ||
} | ||
}); | ||
}; | ||
module.exports = Graylog2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.