Comparing version 0.0.2 to 0.1.0
150
graylog.js
@@ -1,24 +0,32 @@ | ||
var zlib = require('zlib'), | ||
crypto = require('crypto'), | ||
async = require('async'), | ||
dgram = require('dgram'); | ||
var zlib = require('zlib'), | ||
crypto = require('crypto'), | ||
dgram = require('dgram'), | ||
util = require('util'), | ||
EventEmitter = require('events').EventEmitter; | ||
/** | ||
* Graylog instances emit errors. That means you really really should listen for them, | ||
* or accept uncaught exceptions (node throws if you don't listen for "error"). | ||
*/ | ||
var graylog = function graylog(config) { | ||
EventEmitter.call(this); | ||
var key; | ||
this.config = config; | ||
this.config = config; | ||
this.servers = config.servers; | ||
this.client = null; | ||
this.hostname = config.hostname || require('os').hostname(); | ||
this.facility = config.facility || 'Node.js'; | ||
this.servers = config.servers; | ||
this.hostname = config.hostname || require('os').hostname(); | ||
this.facility = config.facility || 'Node.js'; | ||
this._callCount = 0; | ||
this._isDestroyed = false; | ||
this._callCount = 0; | ||
this._bufferSize = config.bufferSize || this.DEFAULT_BUFFERSIZE; | ||
this._dataSize = this._bufferSize - 12; | ||
this._bufferSize = config.bufferSize || this.DEFAULT_BUFFERSIZE; | ||
}; | ||
graylog.prototype.DEFAULT_BUFFERSIZE = 8192; | ||
util.inherits(graylog, EventEmitter); | ||
graylog.prototype.DEFAULT_BUFFERSIZE = 1400; // a bit less than a typical MTU of 1500 to be on the safe side | ||
graylog.prototype.level = { | ||
@@ -40,2 +48,18 @@ EMERG: 0, // system is unusable | ||
graylog.prototype.getClient = function () { | ||
if (!this.client && !this._isDestroyed) { | ||
this.client = dgram.createSocket("udp4"); | ||
} | ||
return this.client; | ||
}; | ||
graylog.prototype.destroy = function () { | ||
if (this.client) { | ||
this.client.close(); | ||
this.client = null; | ||
this._isDestroyed = true; | ||
} | ||
}; | ||
graylog.prototype.emergency = function (short_message, full_message, additionalFields, timestamp) { | ||
@@ -111,3 +135,3 @@ return this._log(short_message, full_message, additionalFields, timestamp, this.level.EMERGENCY); | ||
else { | ||
message.full_message = message.short_message = JSON.stringify(short_message); | ||
message.full_message = message.short_message = JSON.stringify(short_message); | ||
} | ||
@@ -131,55 +155,65 @@ | ||
if (err) { | ||
return; | ||
return that.emit('error', err); | ||
} | ||
var chunkCount = Math.ceil(buffer.length / that._bufferSize), | ||
server = that.getServer(), | ||
client = dgram.createSocket("udp4"); | ||
// If it all fits, just send it | ||
if (buffer.length <= that._bufferSize) { | ||
return that.send(buffer, that.getServer()); | ||
} | ||
// It didn't fit, so prepare for a chunked stream | ||
var bufferSize = that._bufferSize, | ||
dataSize = bufferSize - 12, // the data part of the buffer is the buffer size - header size | ||
chunkCount = Math.ceil(buffer.length / dataSize); | ||
if (chunkCount > 128) { | ||
return console.error('Cannot send messages bigger than 1022.5K, not sending'); | ||
return that.emit('error', new Error('Cannot log messages bigger than ' + (dataSize * 128) + ' bytes')); | ||
} | ||
// If it all fits, just send it | ||
if (chunkCount === 1) { | ||
return that.send(buffer, client, server, function () { | ||
client.close(); | ||
}); | ||
} | ||
// Generate a random id in buffer format | ||
crypto.randomBytes(8, function (err, id) { | ||
if (err) { | ||
return that.emit('error', err); | ||
} | ||
// To be tested: whats faster, sending as we go or prebuffering? | ||
var chunk = new Buffer(that._bufferSize), | ||
start = 0, | ||
stop = 0, | ||
// To be tested: what's faster, sending as we go or prebuffering? | ||
var server = that.getServer(), | ||
chunk = new Buffer(bufferSize), | ||
chunkSequenceNumber = 0; | ||
// Set up magic number (0 and 1) and chunk total count (11) | ||
// Prepare the header | ||
// Set up magic number (bytes 0 and 1) | ||
chunk[0] = 30; | ||
chunk[1] = 15; | ||
// Set the total number of chunks (byte 11) | ||
chunk[11] = chunkCount; | ||
// set message id (2,9) | ||
// Set message id (bytes 2-9) | ||
id.copy(chunk, 2, 0, 8); | ||
async.whilst( | ||
function () { return chunkSequenceNumber < chunkCount; }, | ||
function (cb) { | ||
// Set chunk sequence number | ||
chunk[10] = chunkSequenceNumber; | ||
function send(err) { | ||
if (err || chunkSequenceNumber >= chunkCount) { | ||
// We have reached the end, or had an error (which will already have been emitted) | ||
return; | ||
} | ||
// Select data from full buffer | ||
start = chunkSequenceNumber * that._dataSize; | ||
stop = Math.min((chunkSequenceNumber + 1) * that._dataSize, buffer.length); | ||
buffer.copy(chunk, 12, start, stop); | ||
// Set chunk sequence number (byte 10) | ||
chunk[10] = chunkSequenceNumber; | ||
chunkSequenceNumber++; | ||
// Copy data from full buffer into the chunk | ||
var start = chunkSequenceNumber * dataSize; | ||
var stop = Math.min((chunkSequenceNumber + 1) * dataSize, buffer.length); | ||
// Send a chunk of 8192 bytes or less | ||
that.send(chunk.slice(0, stop - start + 12), client, server, cb); | ||
}, | ||
function () { client.close(); } | ||
); | ||
buffer.copy(chunk, 12, start, stop); | ||
chunkSequenceNumber++; | ||
// Send the chunk | ||
that.send(chunk.slice(0, stop - start + 12), server, send); | ||
} | ||
send(); | ||
}); | ||
@@ -189,9 +223,21 @@ }); | ||
graylog.prototype.send = function (chunk, client, server, cb) { | ||
client.send(chunk, 0, chunk.length, server.port, server.host, function (err, byteCount) { | ||
graylog.prototype.send = function (chunk, server, cb) { | ||
var that = this, | ||
client = this.getClient(); | ||
if (!client) { | ||
var error = new Error('Socket was already destroyed'); | ||
this.emit('error', error); | ||
return cb(error); | ||
} | ||
client.send(chunk, 0, chunk.length, server.port, server.host, function (err/*, bytes */) { | ||
if (err) { | ||
console.log(err); | ||
that.emit('error', err); | ||
} | ||
cb(); | ||
if (cb) { | ||
cb(err); | ||
} | ||
}); | ||
@@ -198,0 +244,0 @@ }; |
{ | ||
"name": "graylog2", | ||
"description": "Graylog2 client library for node.js", | ||
"homepage" : "http://egorfine.github.com/node-graylog", | ||
"bugs" : { | ||
"url" : "http://github.com/stelcheck/node-graylog2/issues" | ||
"homepage": "http://egorfine.github.com/node-graylog", | ||
"bugs": { | ||
"url": "http://github.com/Wizcorp/node-graylog2/issues" | ||
}, | ||
"version": "0.0.2", | ||
"author": "Marc Trudel-Belise", | ||
"version": "0.1.0", | ||
"author": "Marc Trudel-Belisle", | ||
"engines": { | ||
"node": ">=0.6.11" | ||
"node": ">=0.6.11" | ||
}, | ||
"dependencies": { | ||
"async": "0.1.22" | ||
}, | ||
"devDependencies": { | ||
@@ -21,3 +18,3 @@ "jshint": "0.9.1" | ||
"type": "git", | ||
"url": "http://github.com/stelcheck/node-graylog2.git" | ||
"url": "http://github.com/Wizcorp/node-graylog2.git" | ||
}, | ||
@@ -27,3 +24,3 @@ "scripts": { | ||
}, | ||
"main" : "./graylog.js" | ||
"main": "./graylog.js" | ||
} |
@@ -10,3 +10,4 @@ # node-graylog2 | ||
** New: ** Chunked GELF is now supported. | ||
** New: ** Chunked [GELF](https://github.com/Graylog2/graylog2-docs/wiki/GELF) | ||
is now supported. | ||
@@ -27,12 +28,21 @@ ## Synopsis | ||
### Code snippets | ||
```javascript | ||
graylog2 = require("graylog2"); | ||
logger = new graylog2.graylog({ | ||
servers : [ | ||
{ 'host': 127.0.0.1, port: 12201 }, | ||
{ 'host': 127.0.0.2, port: 12201 } | ||
], | ||
hostname : 'server.name', // (optional) | ||
facility : 'Node.js' // (optional) | ||
}); | ||
var graylog2 = require("graylog2"); | ||
var logger = new graylog2.graylog({ | ||
servers: [ | ||
{ 'host': 127.0.0.1, port: 12201 }, | ||
{ 'host': 127.0.0.2, port: 12201 } | ||
], | ||
hostname: 'server.name', // the name of this host | ||
// (optional, default: os.hostname()) | ||
facility: 'Node.js', // the facility for these log messages | ||
// (optional, default: "Node.js") | ||
bufferSize: 1350 // max UDP packet size, should never exceed the | ||
// MTU of your system (optional, default: 1400) | ||
}); | ||
logger.on('error', function (error) { | ||
console.error('Error while trying to write to graylog2:', error); | ||
}); | ||
``` | ||
@@ -43,3 +53,3 @@ | ||
```javascript | ||
logger.log("What we've got here is...failure to communicate"); | ||
logger.log("What we've got here is...failure to communicate"); | ||
``` | ||
@@ -50,5 +60,5 @@ | ||
```javascript | ||
logger.log("What we've got here is...failure to communicate", "Some men you just | ||
can't reach. So you get what we had here last week, which is the way he wants | ||
it... well, he gets it. I don't like it any more than you men."); | ||
logger.log("What we've got here is...failure to communicate", "Some men you just | ||
can't reach. So you get what we had here last week, which is the way he wants | ||
it... well, he gets it. I don't like it any more than you men."); | ||
``` | ||
@@ -59,3 +69,3 @@ | ||
```javascript | ||
logger.log("What we've got here is...failure to communicate", { cool: 'beans' }); | ||
logger.log("What we've got here is...failure to communicate", { cool: 'beans' }); | ||
``` | ||
@@ -66,9 +76,9 @@ | ||
```javascript | ||
logger.log("What we've got here is...failure to communicate", "Some men you just | ||
can't reach. So you get what we had here last week, which is the way he wants | ||
it... well, he gets it. I don't like it any more than you men.", | ||
{ | ||
cool: "beans" | ||
} | ||
); | ||
logger.log("What we've got here is...failure to communicate", "Some men you just | ||
can't reach. So you get what we had here last week, which is the way he wants | ||
it... well, he gets it. I don't like it any more than you men.", | ||
{ | ||
cool: "beans" | ||
} | ||
); | ||
``` | ||
@@ -78,3 +88,3 @@ | ||
See <code>test.js</code>. | ||
See `test.js`. | ||
@@ -81,0 +91,0 @@ ## What is graylog2 after all? |
11
test.js
@@ -6,4 +6,3 @@ var graylog = require('./graylog'), | ||
servers = [ | ||
{ 'host': '192.168.100.85', 'port': 12201 }, | ||
{ 'host': '192.168.100.86', 'port': 12201 } | ||
{ 'host': '127.0.0.1', 'port': 12201 } | ||
]; | ||
@@ -37,3 +36,3 @@ | ||
console.log('---------------------------------------------'); | ||
for (var i = 4; i <= 512; i *= 2) { | ||
for (var i = 4; i <= 128; i *= 2) { | ||
file = './data/' + i + '.dat'; | ||
@@ -46,4 +45,8 @@ data = fs.readFileSync(file); | ||
console.log('Insertion complete. Please check', 'http://'+servers[0].host+':3000', 'and verify that insertion was successfull'); | ||
console.log('Insertion complete. Please check', 'http://' + servers[0].host + ':3000', 'and verify that insertion was successfull'); | ||
console.log(''); | ||
setTimeout(function () { | ||
client.destroy(); | ||
}, 2000); | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 2 instances in 1 package
0
100
9
343976
20
228
- Removedasync@0.1.22
- Removedasync@0.1.22(transitive)