ain2
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -29,4 +29,4 @@ # CHANGELOG | ||
* If you want to have singleton logger, use | ||
* If you want to have singleton logger, use | ||
var logger = require('ain2').getInstance(); | ||
@@ -33,0 +33,0 @@ |
215
index.js
@@ -37,7 +37,7 @@ var dgram = require('dgram'); | ||
} | ||
var getSocket = function () { | ||
var getSocket = function (type) { | ||
if (undefined === socket) { | ||
socket = dgram.createSocket('udp4') | ||
socket = dgram.createSocket(type); | ||
@@ -77,53 +77,47 @@ socket.on('error', socketErrorHandler) | ||
var Transport = { | ||
UDP: function(message, severity) { | ||
var client = dgram.createSocket('udp4'); | ||
var self = this; | ||
var syslogMessage = this.composerFunction(message, severity); | ||
client.send(syslogMessage, | ||
0, | ||
syslogMessage.length, | ||
this.port, | ||
this.address, | ||
function(err, bytes) { | ||
self._logError(err, bytes); | ||
client.close(); | ||
} | ||
); | ||
getSocket('udp4').send(syslogMessage, | ||
0, | ||
syslogMessage.length, | ||
this.port, | ||
this.address, | ||
function(err, bytes) { | ||
self._logError(err, bytes); | ||
releaseSocket(); | ||
} | ||
); | ||
}, | ||
unix_dgram: function(message, severity){ | ||
var self = this; | ||
var preambleBuffer = self.composerFunction('', severity); | ||
var formattedMessageBuffer = Buffer.isBuffer(message) ? message : new Buffer(message); | ||
var chunkSize = 2000 - preambleBuffer.length - 200; | ||
var numChunks = Math.ceil(formattedMessageBuffer.length / chunkSize); | ||
file: (function() { | ||
var logTarget ; | ||
switch(require('os').type()) { | ||
case 'Darwin': | ||
case 'FreeBSD': | ||
logTarget = '/var/run/syslog' ; | ||
break ; | ||
case 'Linux': | ||
logTarget = '/dev/log' ; | ||
break ; | ||
default: | ||
logTarget = false ; | ||
break ; | ||
var fragments = [preambleBuffer]; | ||
if (numChunks > 1){ | ||
for (var i = 0; i < numChunks; i++){ | ||
fragments.push(formattedMessageBuffer.slice(i * chunkSize, Math.min(formattedMessageBuffer.length, (i + 1) * chunkSize)), | ||
new Buffer(' [' + (i + 1) + '/' + numChunks + ']', 'ascii')); | ||
} | ||
} else{ | ||
fragments.push(formattedMessageBuffer); | ||
} | ||
return function(message, severity) { | ||
if (false === logTarget) { | ||
throw new Error('Unknown OS Type: ' + require('os').type()) ; | ||
} | ||
var client = dgram.createSocket('unix_dgram') ; | ||
var syslogMessage = this.composerFunction(message, severity); | ||
client.send(syslogMessage, | ||
0, | ||
syslogMessage.length, | ||
logTarget, | ||
this._logError | ||
); | ||
client.close() ; | ||
}; | ||
})() | ||
var chunk = Buffer.concat(fragments); | ||
var socket = getSocket('unix_dgram'); | ||
socket.send(chunk, | ||
0, | ||
chunk.length, | ||
this.path, | ||
function(err, bytes){ | ||
self._logError(err, bytes); | ||
releaseSocket(); | ||
} | ||
); | ||
} | ||
}; | ||
@@ -171,33 +165,33 @@ | ||
function format(f) { | ||
var util = require('util'), | ||
i = 0; | ||
var util = require('util'), | ||
i = 0; | ||
if (typeof f !== 'string') { | ||
var objects = []; | ||
for (i = 0; i < arguments.length; i++) { | ||
objects.push(util.inspect(arguments[i])); | ||
if (typeof f !== 'string') { | ||
var objects = []; | ||
for (i = 0; i < arguments.length; i++) { | ||
objects.push(util.inspect(arguments[i])); | ||
} | ||
return objects.join(' '); | ||
} | ||
return objects.join(' '); | ||
} | ||
i = 1; | ||
var args = arguments; | ||
var str = String(f).replace(formatRegExp, function(x) { | ||
switch (x) { | ||
case '%s': return String(args[i++]); | ||
case '%d': return Number(args[i++]); | ||
case '%j': return JSON.stringify(args[i++]); | ||
default: | ||
return x; | ||
i = 1; | ||
var args = arguments; | ||
var str = String(f).replace(formatRegExp, function(x) { | ||
switch (x) { | ||
case '%s': return String(args[i++]); | ||
case '%d': return Number(args[i++]); | ||
case '%j': return JSON.stringify(args[i++]); | ||
default: | ||
return x; | ||
} | ||
}); | ||
for (var len = args.length, x = args[i]; i < len; x = args[++i]) { | ||
if (x === null || typeof x !== 'object') { | ||
str += ' ' + x; | ||
} else { | ||
str += ' ' + util.inspect(x); | ||
} | ||
} | ||
}); | ||
for (var len = args.length, x = args[i]; i < len; x = args[++i]) { | ||
if (x === null || typeof x !== 'object') { | ||
str += ' ' + x; | ||
} else { | ||
str += ' ' + util.inspect(x); | ||
} | ||
} | ||
return str; | ||
return str; | ||
} | ||
@@ -213,5 +207,5 @@ | ||
this._logError = function(err, other) { | ||
if(err){ | ||
nodeConsole.error('Cannot log message via %s:%d', this.hostname, this.port); | ||
} | ||
if(err){ | ||
nodeConsole.error('Cannot log message via %s:%d', this.hostname, this.port); | ||
} | ||
}.bind(this); | ||
@@ -223,5 +217,5 @@ this.set(config); | ||
/** | ||
* Get singleton instance of SysLogger. | ||
* @returns {SysLogger} | ||
*/ | ||
* Get singleton instance of SysLogger. | ||
* @returns {SysLogger} | ||
*/ | ||
SysLogger.getInstance = function() { | ||
@@ -241,2 +235,3 @@ if(!SingletonInstance){ | ||
* - hostname - {String} By default is require("os").hostname() | ||
* - address - {String} By default is 127.0.0.1 | ||
* - port - {Number} Defaults to 514 | ||
@@ -251,9 +246,7 @@ * - transport - {Transport|String} Defaults to Transport.UDP | ||
this.setHostname(config.hostname); | ||
this.setAddress(config.address); | ||
this.setPort(config.port); | ||
this.setPath(config.path); | ||
this.setMessageComposer(config.messageComposer); | ||
if (config.hostname) { | ||
this.setTransport(Transport.UDP) ; | ||
} else { | ||
this.setTransport(config.transport) ; | ||
} | ||
this.setTransport(Transport.UDP); | ||
@@ -265,5 +258,8 @@ return this; | ||
this.transport = transport || Transport.UDP; | ||
if (typeof this.transport == 'string') { | ||
if (typeof this.transport === 'string') { | ||
this.transport = Transport[this.transport] ; | ||
} | ||
if (typeof this.path === 'string' && this.path.length > 0){ | ||
this.transport = Transport.unix_dgram | ||
} | ||
return this; | ||
@@ -276,2 +272,3 @@ }; | ||
}; | ||
SysLogger.prototype.setFacility = function(facility) { | ||
@@ -284,9 +281,10 @@ this.facility = facility || Facility.user; | ||
}; | ||
SysLogger.prototype.setAddress = function(address) { | ||
this.address = address || DefaultAddress; | ||
return this; | ||
}; | ||
SysLogger.prototype.setHostname = function(hostname) { | ||
if (hostname) { | ||
this.hostname = this.address = hostname; | ||
} else { | ||
this.hostname = DefaultHostname; | ||
this.address = DefaultAddress; | ||
} | ||
this.hostname = hostname || DefaultHostname; | ||
return this; | ||
@@ -300,2 +298,16 @@ }; | ||
SysLogger.prototype.setPath = function(path) { | ||
this.path = path || ''; | ||
if (typeof this.path === 'string' && this.path.length > 0){ | ||
try { | ||
dgram = require('unix-dgram'); | ||
} catch(err){ | ||
throw new Error('unix-dgram module not installed, cannot specify a unix socket path'); | ||
} | ||
} | ||
return this; | ||
}; | ||
SysLogger.prototype.setMessageComposer = function(composerFunction){ | ||
@@ -323,3 +335,3 @@ this.composerFunction = composerFunction || this.composeSyslogMessage; | ||
if (typeof severity == 'string'){ | ||
severity = Severity[severity]; | ||
severity = Severity[severity]; | ||
} | ||
@@ -366,4 +378,4 @@ this._send(message, severity); | ||
return new Buffer('<' + (this.facility * 8 + severity) + '>' + | ||
this.getDate() + ' ' + this.hostname + ' ' + | ||
this.tag + '[' + process.pid + ']:' + message); | ||
this.getDate() + ' ' + this.hostname + ' ' + | ||
this.tag + '[' + process.pid + ']:' + message); | ||
} | ||
@@ -408,13 +420,9 @@ | ||
var dt = new Date(); | ||
var hours = this.leadZero(dt.getHours()); | ||
var minutes = this.leadZero(dt.getMinutes()); | ||
var seconds = this.leadZero(dt.getSeconds()); | ||
var month = dt.getMonth(); | ||
var day = dt.getDate(); | ||
if(day < 10){ | ||
day = ' ' + day; | ||
} | ||
var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', | ||
'Sep', 'Oct', 'Nov', 'Dec' ]; | ||
return months[month] + " " + day + " " + hours + ":" + minutes + ":" + seconds; | ||
var hours = this.leadZero(dt.getUTCHours()); | ||
var minutes = this.leadZero(dt.getUTCMinutes()); | ||
var seconds = this.leadZero(dt.getUTCSeconds()); | ||
var month = this.leadZero((dt.getUTCMonth() + 1)); | ||
var day = this.leadZero(dt.getUTCDate()); | ||
var year = dt.getUTCFullYear(); | ||
return year+'-'+month+'-'+day+' '+hours+':'+minutes+':'+seconds; | ||
} | ||
@@ -431,2 +439,1 @@ | ||
module.exports = SysLogger; | ||
{ | ||
"name" : "ain2", | ||
"description" : "Syslog logging for node.js. Continuation of ain", | ||
"version" : "1.2.1", | ||
"main" : "./index", | ||
"author" : "Alexander Dorofeev <aka.spin@gmail.com>", | ||
"contributors" : [ | ||
{ | ||
"name" : "Alexander Dorofeev", | ||
"email" : "aka.spin@gmail.com" | ||
}, | ||
{ | ||
"name" : "Patrick Huesler", | ||
"email" : "patrick.huesler@googlemail.com" | ||
}, | ||
{ | ||
"name" : "Mark Wubben", | ||
"email" : "mark@novemberborn.net" | ||
}, | ||
{ | ||
"name" : "Parham Michael Ossareh", | ||
"email" : "ossareh@gmail.com" | ||
}, | ||
{ | ||
"name" : "Carlos Lage", | ||
"email" : "carlos.lage@livestream.com" | ||
}, | ||
{ | ||
"name" : "J. Maurice", | ||
"email" : "j@wiz.biz" | ||
} | ||
], | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "http://github.com/phuesler/ain.git" | ||
"name": "ain2", | ||
"description": "Syslog logging for node.js. Continuation of ain", | ||
"version": "1.3.0", | ||
"main": "./index", | ||
"author": "Alexander Dorofeev <aka.spin@gmail.com>", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha test/**/*.spec.js" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Alexander Dorofeev", | ||
"email": "aka.spin@gmail.com" | ||
}, | ||
"bugs" : { | ||
"url" : "http://github.com/phuesler/ain/issues" | ||
{ | ||
"name": "Patrick Huesler", | ||
"email": "patrick.huesler@googlemail.com" | ||
}, | ||
"licenses" : [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://github.com/phuesler/ain/master/LICENSE" | ||
} | ||
] | ||
{ | ||
"name": "Mark Wubben", | ||
"email": "mark@novemberborn.net" | ||
}, | ||
{ | ||
"name": "Parham Michael Ossareh", | ||
"email": "ossareh@gmail.com" | ||
}, | ||
{ | ||
"name": "Carlos Lage", | ||
"email": "carlos.lage@livestream.com" | ||
}, | ||
{ | ||
"name": "J. Maurice", | ||
"email": "j@wiz.biz" | ||
} | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/phuesler/ain.git" | ||
}, | ||
"bugs": { | ||
"url": "http://github.com/phuesler/ain/issues" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://github.com/phuesler/ain/master/LICENSE" | ||
} | ||
], | ||
"devDependencies": { | ||
"mocha": "~1.12.0", | ||
"chai": "~1.7.2" | ||
}, | ||
"optionalDependencies": { | ||
"unix-dgram": "0.0.3" | ||
} | ||
} |
@@ -12,5 +12,3 @@ # ain* | ||
*Ain* can send messages by UDP to `127.0.0.1:514` or to the a unix socket; | ||
/dev/log on Linux and /var/run/syslog on Mac OS X. The unix sockets only | ||
work for the 0.4.x versions of node.js, unix_dgram sockets support has | ||
been [removed](http://groups.google.com/group/nodejs/browse_thread/thread/882ce172ec463f52/62e392bb0f32a7cb) from > 0.5.x. | ||
/dev/log on Linux and /var/run/syslog on Mac OS X. Unix socket support is possible if [unix-dgram](https://npmjs.org/package/unix-dgram) can be built and installed. | ||
@@ -72,3 +70,3 @@ *In the Phoenician alphabet letter "ain" indicates eye. | ||
* `PORT` - 514 | ||
* `Transport` - UDP or Unix socket | ||
* `Transport` - UDP | ||
@@ -92,3 +90,4 @@ You can change them by passing in the params to the constructor or by | ||
* port - defaults to 514 | ||
* transport - defaults to 'UDP', can also be 'file' | ||
* transport - defaults to 'UDP' | ||
* path - path to filesystem socket if using unix_dgram transport | ||
* messageComposer - a custom function to compose syslog messages | ||
@@ -193,1 +192,6 @@ | ||
default, the severity level is *notice*. | ||
# Development | ||
npm install | ||
npm test |
@@ -1,7 +0,9 @@ | ||
function executeDifferentLogCalls(logger){ | ||
logger.log('log'); | ||
logger.info('info'); | ||
logger.warn('warn'); | ||
logger.error('error'); | ||
logger.debug('debug'); | ||
function executeDifferentLogCalls(logger, limit){ | ||
for(var i = 0; i < limit; i++){ | ||
logger.log('log'); | ||
logger.info('info'); | ||
logger.warn('warn'); | ||
logger.error('error'); | ||
logger.debug('debug'); | ||
}; | ||
} | ||
@@ -13,3 +15,3 @@ | ||
executeDifferentLogCalls(logger); | ||
executeDifferentLogCalls(logger, 500); | ||
@@ -24,3 +26,3 @@ logger.setMessageComposer(function(message, severity){ | ||
process.exit(); | ||
}, 500); | ||
}, 1000); | ||
@@ -34,8 +36,5 @@ } | ||
var server = dgram.createSocket("udp4"); | ||
var messages = []; | ||
server.on("message", function (msg, rinfo) { | ||
console.log(msg.toString()); | ||
messages.push(msg.toString()); | ||
}); | ||
@@ -42,0 +41,0 @@ |
# TODO | ||
* TCP support | ||
* Tests. I feel ashamed to not have tests in this project |
Sorry, the diff of this file is not supported yet
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
32631
14
743
194
1
2
2
7