node-logentries
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -6,9 +6,7 @@ /* Copyright (c) 2011-2013 Richard Rodger, BSD License */ | ||
var util = require('util') | ||
var http = require('http') | ||
var net = require('net') | ||
var tls = require('tls') | ||
var events = require('events') | ||
var MARK = 'logentries: ' | ||
function LogEntriesTransport( opts, logger ) { | ||
@@ -19,3 +17,5 @@ var self = this | ||
var connected = false | ||
var req = null | ||
var connecting = false | ||
var socket = null | ||
var usequotes = !!opts.usequotes | ||
@@ -26,3 +26,8 @@ function process() { | ||
var logline = opts.token + '"' + entry.join('" "') + '"\n'; | ||
var logline = opts.token | ||
if (usequotes) { | ||
logline += '"' + entry.join('" "') + '"\n' | ||
} else { | ||
logline += ' ' + entry.join(' ') + '\n' | ||
} | ||
@@ -32,3 +37,3 @@ try { | ||
logger.emit('log',logline) | ||
req.write(logline) | ||
socket.write(logline) | ||
} | ||
@@ -39,2 +44,3 @@ catch(e) { | ||
connected = false | ||
connecting = false | ||
break | ||
@@ -47,5 +53,5 @@ } | ||
function connect() { | ||
if( req ) { | ||
if( socket ) { | ||
try { | ||
req.end() | ||
socket.end() | ||
} | ||
@@ -58,4 +64,4 @@ catch( e ) { | ||
var options = { | ||
host: 'api.logentries.com', | ||
port: 10000, | ||
host: opts.host || 'api.logentries.com', | ||
port: opts.secure ? 20000 : 10000, | ||
token: opts.token | ||
@@ -66,37 +72,43 @@ } | ||
req = http.request(options, function(res) { | ||
if( 200 != res.statusCode ) { | ||
res.setEncoding('utf8'); | ||
if (opts.secure) { | ||
socket = tls.connect(options.port, options.host, secureConnection) | ||
} | ||
else { | ||
socket = net.createConnection(options.port, options.host) | ||
} | ||
var err = { | ||
statusCode: res.statusCode, | ||
headers: res.headers | ||
} | ||
connecting = true | ||
var body = [] | ||
res.on('data',function(chunk){ | ||
body.push(chunk) | ||
}) | ||
function handleConnection() { | ||
connecting = false | ||
connected = true | ||
process() | ||
} | ||
res.on('end',function(){ | ||
try { | ||
err.body = JSON.parse(body.join('')) | ||
} | ||
catch(e) { | ||
err.body = {msg:body.join('')} | ||
} | ||
logger.emit('error',err) | ||
}) | ||
function handleError(e) { | ||
logger.emit('error',e) | ||
connected = false | ||
connecting = false | ||
} | ||
function secureConnection() { | ||
if (!socket.authorized) { | ||
/* | ||
* We need to check this as the tls module will accept all | ||
* certs by default. Nobody likes a man in the middle attack. | ||
*/ | ||
handleError(new Error(socket.authorizationError)) | ||
} else { | ||
handleConnection() | ||
} | ||
}); | ||
} | ||
req.on('error', function(e) { | ||
logger.emit('error',e) | ||
socket.on('connect', handleConnection); | ||
socket.on('error', handleError) | ||
socket.on('close', function() { | ||
connected = false | ||
connecting = false | ||
}); | ||
if( null != req ) { | ||
connected = true | ||
process() | ||
} | ||
} | ||
@@ -113,3 +125,3 @@ | ||
} | ||
else { | ||
else if (!connecting) { | ||
connect() | ||
@@ -120,5 +132,5 @@ } | ||
self.end = function() { | ||
if( req ) { | ||
if( socket ) { | ||
try { | ||
req.end() | ||
socket.end() | ||
} | ||
@@ -141,2 +153,4 @@ catch( e ) { | ||
* printerror: true; print errors to STDERR with console.error | ||
* secure: false; Use tls for communication | ||
* flatten: true; JSON entries will be flattened. | ||
*/ | ||
@@ -150,2 +164,3 @@ function Logger( opts ) { | ||
opts.printerror = 'undefined' == typeof(opts.printerror) ? true : opts.printerror | ||
opts.flatten = 'undefined' == typeof(opts.flatten) ? true : opts.flatten | ||
@@ -190,3 +205,16 @@ // register at least one listener for 'error' as logging failure should not bring down server | ||
function flatten(json, prefix) { | ||
var result = "" | ||
Object.keys(json).forEach(function(key) { | ||
var value = json[key] | ||
if( 'object' == typeof( value ) || Array.isArray(value) ) { | ||
result += flatten(value, prefix + key + ".") | ||
} else { | ||
result += prefix + key + "=" + value + " " | ||
} | ||
}) | ||
return result | ||
} | ||
self.log = function() { | ||
@@ -208,17 +236,16 @@ var args = Array.prototype.slice.call(arguments) | ||
args[1] = data.toISOString(); | ||
} | ||
} | ||
else if( 'object' == typeof( data ) || Array.isArray(data) ) { | ||
args[1] = JSON.stringify(data); | ||
args[1] = opts.flatten ? flatten(data, '') : JSON.stringify(data) | ||
} | ||
else { | ||
args[1] = ''+data; | ||
args[1] = ''+data | ||
} | ||
//Replace newlines with unicode line separator | ||
args[1] = args[1].replace(/\n/g, "\u2028"); | ||
//Replace newlines with unicode line separator | ||
args[1] = args[1].replace(/\n/g, "\u2028") | ||
if (timestamp) { | ||
var t = new Date().toISOString(); | ||
args.unshift(t); | ||
var t = new Date().toISOString() | ||
args.unshift(t) | ||
} | ||
@@ -225,0 +252,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"keywords": ["log", "logging", "winston", "logentries", "logentries.com", "wrapper","api"], | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/rjrodger/node-logentries", | ||
@@ -28,2 +28,2 @@ "author": "Richard Rodger <richard@ricebridge.com> (http://richardrodger.com/)", | ||
} | ||
} | ||
} |
@@ -7,3 +7,3 @@ # node-logentries | ||
Current Version: 0.0.2 | ||
Current Version: 0.1.1 | ||
@@ -140,2 +140,3 @@ Tested on: node 0.4.9 | ||
* _token_: required; logentries destination token uuid | ||
* _secure_: optional; default is false; use tls for communication | ||
* _transport_: optional; default is LogEntriesTransport; transport object | ||
@@ -145,2 +146,3 @@ * _levels_: optional; default is syslog-style; custom log levels | ||
* _timestamp_: optional; default is true; autogenerate a timestamp | ||
* _usequotes_: optional; default is false; add double quotes around every field | ||
@@ -299,3 +301,3 @@ The _token_ entry relates to your logentries.com configuration. The _transport_ option allows you to | ||
The acceptance tests (these push actual data to the live logentries.com service) are simple node scripts, and are in the _accept_ folder. | ||
Copy the _accept/conf.js_ file to _accept/conf.mine.js_ and add your logentries.com user keys. Run directly: | ||
Copy the _accept/conf.js_ file to _accept/conf.mine.js_ and add the token for your log file. Run directly: | ||
@@ -302,0 +304,0 @@ node live.accept.js |
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
18657
246
309
3