Comparing version 0.0.14 to 0.1.0
@@ -1,5 +0,10 @@ | ||
var zlib = require('zlib'), | ||
dgram = require('dgram'), | ||
util = require('util'); | ||
var | ||
zlib = require('zlib'), | ||
dgram = require('dgram'), | ||
util = require('util'), | ||
dns = require('dns'); | ||
var | ||
isIpRegexp = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/; | ||
GLOBAL.LOG_EMERG=0; // system is unusable | ||
@@ -21,6 +26,12 @@ GLOBAL.LOG_ALERT=1; // action must be taken immediately | ||
GLOBAL.graylogSequence = 0; | ||
GLOBAL.graylogChunkSize = 1100; // 8192 is the maximum | ||
function generateMessageId() { | ||
return '' + (Date.now() + Math.floor(Math.random()*10000)); | ||
} | ||
function _logToConsole(shortMessage, opts) { | ||
var consoleString = shortMessage; | ||
var | ||
consoleString = shortMessage, | ||
additionalFields = []; | ||
@@ -31,3 +42,2 @@ if (opts.full_message) { | ||
var additionalFields = []; | ||
Object.keys(opts).forEach(function(key) { | ||
@@ -53,2 +63,54 @@ if (key[0]=='_' && key!="_logSequence") { | ||
function sendChunked(graylog2Client, compressedMessage, address) { | ||
var | ||
messageId = generateMessageId(), | ||
sequenceSize = Math.ceil(compressedMessage.length / GLOBAL.graylogChunkSize), | ||
byteOffset = 0, | ||
chunksWritten = 0; | ||
if (sequenceSize > 128) { | ||
util.debug("Graylog oops: log message is larger than 128 chunks, I print to stderr and give up: \n" + message.toString()); | ||
return; | ||
} | ||
for(var sequence=0; sequence<sequenceSize; sequence++) { | ||
var | ||
chunkBytes = (byteOffset + GLOBAL.graylogChunkSize) < compressedMessage.length ? GLOBAL.graylogChunkSize : (compressedMessage.length - byteOffset), | ||
chunk = new Buffer(chunkBytes + 12); | ||
chunk[0] = 0x1e; | ||
chunk[1] = 0x0f; | ||
chunk.write(messageId, 2, 8, 'ascii'); | ||
chunk[10] = sequence; | ||
chunk[11] = sequenceSize; | ||
compressedMessage.copy(chunk, 12, byteOffset, byteOffset+chunkBytes); | ||
byteOffset += chunkBytes; | ||
graylog2Client.send(chunk, 0, chunk.length, GLOBAL.graylogPort, address, function (err, byteCount) { | ||
chunksWritten++; | ||
if (chunksWritten == sequenceSize) { | ||
graylog2Client.close(); | ||
} | ||
}); | ||
} | ||
} | ||
function sendSingleShot(graylog2Client, compressedMessage, address) { | ||
graylog2Client.send(compressedMessage, 0, compressedMessage.length, GLOBAL.graylogPort, address, function (err, byteCount) { | ||
graylog2Client.close(); | ||
}); | ||
} | ||
function resolveAndSend(graylog2Client, compressedMessage, dnsName, sendFunc) { | ||
dns.resolve4(GLOBAL.graylogHost, function(dnsErr, addr) { | ||
if (dnsErr) { | ||
util.debug("Graylog oops: DNS Error (" + dnsErr + "), I print to stderr and give up: \n" + message.toString()); | ||
return; | ||
} | ||
sendFunc(graylog2Client, compressedMessage, addr[0]); | ||
}); | ||
} | ||
function log(shortMessage, a, b) { | ||
@@ -83,3 +145,6 @@ var opts = {}; | ||
var message = new Buffer(JSON.stringify(opts)); | ||
var | ||
message = new Buffer(JSON.stringify(opts)), | ||
sendFunc = sendSingleShot; | ||
zlib.deflate(message, function (err, compressedMessage) { | ||
@@ -90,11 +155,12 @@ if (err) { | ||
if (compressedMessage.length>8192) { // FIXME: support chunked | ||
util.debug("Graylog oops: log message size > 8192, I print to stderr and give up: \n" + message.toString()); | ||
return; | ||
var graylog2Client = dgram.createSocket("udp4"); | ||
if (compressedMessage.length > GLOBAL.graylogChunkSize) { | ||
sendFunc = sendChunked; | ||
} | ||
var graylog2Client = dgram.createSocket("udp4"); | ||
graylog2Client.send(compressedMessage, 0, compressedMessage.length, GLOBAL.graylogPort, GLOBAL.graylogHost, function (err, byteCount) { | ||
graylog2Client.close(); | ||
}); | ||
if (!isIpRegexp.test(GLOBAL.graylogHost)) { | ||
resolveAndSend(graylog2Client, compressedMessage, GLOBAL.graylogHost, sendFunc); | ||
} else { | ||
sendFunc(graylog2Client, compressedMessage, GLOBAL.graylogHost); | ||
} | ||
}); | ||
@@ -113,5 +179,5 @@ } | ||
for(var i=0, len = stack.length; i<len; i++){ | ||
if((match = stack[i].match(/^\s*at\s[^\(]+\(([^\):]+):(\d+):\d+\)/))){ | ||
if(__filename.substr(-match[1].length) == match[1]){ | ||
for (var i=0, len = stack.length; i<len; i++){ | ||
if ((match = stack[i].match(/^\s*at\s[^\(]+\(([^\):]+):(\d+):\d+\)/))){ | ||
if (__filename.substr(-match[1].length) == match[1]){ | ||
continue; | ||
@@ -124,3 +190,2 @@ } | ||
} | ||
} | ||
@@ -127,0 +192,0 @@ |
@@ -6,3 +6,3 @@ { | ||
"bugs" : { "url" : "http://github.com/egorFiNE/node-graylog/issues" }, | ||
"version": "0.0.14", | ||
"version": "0.1.0", | ||
"author": "Egor Egorov", | ||
@@ -14,3 +14,3 @@ "repository": { | ||
"engine": [ "node >=0.6.0" ], | ||
"main" : "./graylog.js" | ||
"main" : "./graylog.js" | ||
} |
@@ -70,2 +70,5 @@ # node-graylog | ||
You can set <code>GLOBAL.graylogChunkSize</code> to the maximum allowed bytes for a single UDP packet. Log messages higher than that will be sent in chunked encoding. | ||
## Example | ||
@@ -87,7 +90,2 @@ | ||
## TODO | ||
* Limit messages size to MTU size?.. | ||
## License | ||
@@ -99,3 +97,3 @@ | ||
Egor Egorov <me@egorfine.com> | ||
Egor Egorov <me@egorfine.com>. | ||
@@ -102,0 +100,0 @@ ## Contributors |
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
Network access
Supply chain riskThis module accesses the network.
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
10412
6
167
102
2