Skip to content

Commit

Permalink
Refactoring and update:
Browse files Browse the repository at this point in the history
* 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
unlucio committed Oct 19, 2014
1 parent 15145e6 commit 04dc076
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
node_js:
- 0.6
- 0.10
36 changes: 0 additions & 36 deletions grunt.js

This file was deleted.

122 changes: 41 additions & 81 deletions lib/winston-graylog2.js
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;
33 changes: 22 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,35 @@
"name": "winston-graylog2",
"description": "A graylog2 transport for winston",
"version": "0.1.10",
"author": "Flite, Inc. http://www.flite.com",
"author": "Started by: Flite, Inc. http://www.flite.com, mantained by: Namshi http://namshi.com",
"contributors": [
{
"name" : "Clay Smith"
}],
{"name": "Luciano Colosio"},
{"name": "Phil Kates"},
{"name": "Clay Smith"},
{"name": "krivchun"},
{"name": "Ville Lautanala"},
{"name": "Alessandro Nadalin"},
{"name": "Róbert Oroszi"},
{"name": "Clay Smith"},
{"name": "Arttu Tervo"}
],
"repository": {
"type": "git",
"url": "http://github.com/flite/winston-graylog2.git"
},
"keywords": ["logging", "sysadmin", "tools", "winston", "graylog2"],
"dependencies": {
"compress-buffer": ">=0.5.1"
},
"devDependencies": {
"winston": "0.4.x",
"vows": "0.5.x"
"winston": "^0.7.3",
"graylog2": "^0.1.3"
},
"main": "./lib/winston-graylog2",
"scripts": { "test": "vows test/*-test.js --spec" },
"engines": { "node": ">= 0.4.0" }
"scripts": {
"test": "mocha -b"
},
"engines": {
"node": ">= 0.10.0"
},
"devDependencies": {
"mocha": "*"
}
}
75 changes: 61 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,81 @@
>
> :-)
A [graylog2][2] transport for [winston][0]. Inspired by [winston-mail][1] transport and [node-graylog][3].
A [graylog2][0] transport for [winston][1] based on the [node-graylog2][2] Library

## Installation
Tested on node-0.6.x, requires npm.
Tested on node-0.10.x, requires npm.

``` sh
$ npm install winston
$ npm install winston-graylog2
```

## Usage
``` js
```javascript
var winston = require('winston');
winston.add(require('winston-graylog2').Graylog2, options);
winston.add(require('winston-graylog2'), options);

```

Options are the following:
or

* __level:__ Level of messages this transport should log. (default: info)
* __silent:__ Boolean flag indicating whether to suppress output. (default: false)
```javascript
var WinstonGraylog2 = require('winston-graylog2');
var logger = new(winston.Logger)({
exitOnError: false,
transports: [new(WinstonGraylog2)(options)]
});
```

## Options

* __name__: Transport name
* __level__: Level of messages this transport should log. (default: info)
* __silent__: Boolean flag indicating whether to suppress output. (default: false)
* __handleExceptions__: Boolean flag, whenever to handle uncaught exceptions. (default: false)
* __graylog__:
- __servers__; list of greylog2 servers
* __host__: your server address (default: localhost)
* __port__: your server port (default: 12201)
- __hostname__: the name of this host (default: os.hostname())
- __facility__: the facility for these log messages (default: "Node.js")
- __bufferSize__: max UDP packet size, should never exceed the MTU of your system (default: 1400)


example:

```javascript
{
name: 'Greylog
level: 'debug',
silet: false,
handleExceptions: false,
graylog: {
servers: [{host: 'localhost', port: 12201}, {host: 'remote.host', port: 12201}],
hostname: 'myServer',
facility: 'myAwesomeApp',
bufferSize: 1400
}
}
```
## Log Levels
Suported log level, as from [node-graylog2][2], are the following
Winstone Level | Graylog2 level
---------------|---------------
emerg | emergency
alert | alert
crit | critical
error | error
warning | warning
notice | notice
info | info
debug | debug
* __graylogHost:__ IP address or hostname of the graylog2 server. (default: localhost)
* __graylogPort:__ Port to send messages to on the graylog2 server. (default: 12201)
* __graylogHostname:__ The hostname associated with graylog2 messages. (default: require('os').hostname())
* __graylogFacility:__ The graylog2 facility to send log messages.. (default: nodejs)
**All other possibile winston's level, or custome level, will default to `info`**

[0]: https://github.com/flatiron/winston
[1]: https://github.com/wavded/winston-mail
[2]: http://www.graylog2.org
[3]: https://github.com/egorFiNE/node-graylog
[1]: http://www.graylog2.org
[2]: https://github.com/Wizcorp/node-graylog2
Loading

0 comments on commit 04dc076

Please sign in to comment.