Comparing version 0.1.0 to 0.2.0
# CHANGELOG | ||
## v0.1.0 | ||
## v0.2.0 | ||
* upgrade object logging to latest format from nodejs (Beresta) | ||
* Support for unix sockets, for the 0.4.x branch of node (Parham Michael | ||
Ossareh/ossareh) | ||
@@ -7,0 +8,0 @@ ## v0.0.3 |
169
index.js
@@ -8,2 +8,83 @@ var dgram = require('dgram'); | ||
function leadZero(n) { | ||
if (n < 10) { | ||
return '0' + n; | ||
} else { | ||
return n; | ||
} | ||
} | ||
/** | ||
* Get current date in syslog format. Thanks https://github.com/kordless/lodge | ||
* @returns {String} | ||
*/ | ||
function getDate() { | ||
var dt = new Date(); | ||
var hours = leadZero(dt.getHours()); | ||
var minutes = leadZero(dt.getMinutes()); | ||
var seconds = 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 Transport = { | ||
UDP: function(message, severity) { | ||
var client = dgram.createSocket('udp4'); | ||
message = new Buffer('<' + (this.facility * 8 + severity) + '>' + | ||
getDate() + ' ' + this.hostname + ' ' + | ||
this.tag + '[' + process.pid + ']:' + message); | ||
client.send(message, | ||
0, | ||
message.length, | ||
this.port, | ||
this.address, | ||
this._logError | ||
); | ||
client.close(); | ||
}, | ||
file: (function() { | ||
var logTarget ; | ||
switch(require('os').type()) { | ||
case 'Darwin': | ||
logTarget = '/var/run/syslog' ; | ||
break ; | ||
case 'Linux': | ||
logTarget = '/dev/log' ; | ||
break ; | ||
default: | ||
throw new Error('Unknown OS Type: ' + require('os').type()) ; | ||
} | ||
return function(message, severity) { | ||
var client = dgram.createSocket('unix_dgram') ; | ||
message = new Buffer('<' + (this.facility * 8 + severity) + '>' + | ||
getDate() + ' ' + this.hostname + ' ' + | ||
this.tag + '[' + process.pid + ']:' + message); | ||
client.send(message, | ||
0, | ||
message.length, | ||
logTarget, | ||
this._logError | ||
); | ||
client.close() ; | ||
}; | ||
})() | ||
}; | ||
var Facility = { | ||
@@ -42,2 +123,3 @@ kern: 0, | ||
var formatRegExp = /%[sdj]/g; | ||
/** | ||
@@ -49,7 +131,8 @@ * Just copy from node.js console | ||
function format(f) { | ||
var util = require('util'); | ||
var util = require('util'), | ||
i = 0; | ||
if (typeof f !== 'string') { | ||
var objects = []; | ||
for (var i = 0; i < arguments.length; i++) { | ||
for (i = 0; i < arguments.length; i++) { | ||
objects.push(util.inspect(arguments[i])); | ||
@@ -61,3 +144,3 @@ } | ||
var i = 1; | ||
i = 1; | ||
var args = arguments; | ||
@@ -83,30 +166,3 @@ var str = String(f).replace(formatRegExp, function(x) { | ||
function leadZero(n) { | ||
if (n < 10) { | ||
return '0' + n; | ||
} else { | ||
return n; | ||
} | ||
} | ||
/** | ||
* Get current date in syslog format. Thanks https://github.com/kordless/lodge | ||
* @returns {String} | ||
*/ | ||
function getDate() { | ||
var dt = new Date(); | ||
var hours = leadZero(dt.getHours()); | ||
var minutes = leadZero(dt.getMinutes()); | ||
var seconds = 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; | ||
} | ||
/** | ||
* Syslog logger | ||
@@ -120,3 +176,3 @@ * @constructor | ||
if(err){ | ||
nodeConsole.error('Cannot connect to %s:%d', this.hostname, this.port); | ||
nodeConsole.error('Cannot log message via %s:%d', this.hostname, this.port); | ||
} | ||
@@ -127,12 +183,23 @@ }.bind(this); | ||
/** | ||
* Init function. All arguments is optional | ||
* @param {String} tag By default is __filename | ||
* @param {Facility|Number|String} By default is "user" | ||
* @param {String} hostname By default is require("os").hostname() | ||
* Init function, takes a configuration object. If a hostname is provided the transport is assumed | ||
* to be Transport.UDP | ||
* @param {Object} configuration object with the following keys: | ||
* - tag - {String} By default is __filename | ||
* - facility - {Facility|Number|String} By default is "user" | ||
* - hostname - {String} By default is require("os").hostname() | ||
* - port - {Number} Defaults to 514 | ||
* - transport - {Transport|String} Defaults to Transport.UDP | ||
*/ | ||
SysLogger.prototype.set = function(tag, facility, hostname, port) { | ||
this.setTag(tag); | ||
this.setFacility(facility); | ||
this.setHostname(hostname); | ||
this.setPort(port); | ||
SysLogger.prototype.set = function(config) { | ||
config = config || {} ; | ||
this.setTag(config.tag); | ||
this.setFacility(config.facility); | ||
this.setHostname(config.hostname); | ||
this.setPort(config.port); | ||
if (config.hostname) { | ||
this.setTransport(Transport.UDP) ; | ||
} else { | ||
this.setTransport(config.transport) ; | ||
} | ||
@@ -142,2 +209,10 @@ return this; | ||
SysLogger.prototype.setTransport = function(transport) { | ||
this.transport = transport || Transport.UDP; | ||
if (typeof this.transport == 'string') { | ||
this.transport = Transport[this.transport] ; | ||
} | ||
return this; | ||
}; | ||
SysLogger.prototype.setTag = function(tag) { | ||
@@ -178,2 +253,3 @@ this.tag = tag || __filename; | ||
}; | ||
/** | ||
@@ -185,14 +261,3 @@ * Send message | ||
SysLogger.prototype._send = function(message, severity) { | ||
var client = dgram.createSocket('udp4'); | ||
message = new Buffer('<' + (this.facility * 8 + severity) + '>' + | ||
getDate() + ' ' + this.hostname + ' ' + | ||
this.tag + '[' + process.pid + ']:' + message); | ||
client.send(message, | ||
0, | ||
message.length, | ||
this.port, | ||
this.address, | ||
this._logError | ||
); | ||
client.close(); | ||
this.transport(message, severity) ; | ||
}; | ||
@@ -199,0 +264,0 @@ |
{ | ||
"name" : "ain2", | ||
"description" : "Syslog logging for node.js. Continuation of ain", | ||
"version" : "0.1.0", | ||
"version" : "0.2.0", | ||
"main" : "./index", | ||
@@ -19,2 +19,6 @@ "author" : "Alexander Dorofeev <aka.spin@gmail.com>", | ||
"email" : "mark@novemberborn.net" | ||
}, | ||
{ | ||
"name" : "Parham Michael Ossareh", | ||
"email" : "ossareh@gmail.com" | ||
} | ||
@@ -21,0 +25,0 @@ ], |
@@ -11,5 +11,6 @@ # ain* | ||
*Ain* send messages by UDP to `127.0.0.1:514` (it's more scalable than | ||
unix domain socket `/dev/log`) in | ||
[RFC 3164](http://www.faqs.org/rfcs/rfc3164.html). | ||
*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. | ||
@@ -52,3 +53,4 @@ *In the Phoenician alphabet letter "ain" indicates eye. | ||
* `HOSTNAME` - localhost | ||
* `HOSTNAME` - 514 | ||
* `PORT` - 514 | ||
* `Transport` - UDP or Unix socket | ||
@@ -58,3 +60,3 @@ You can change them by `set` function. `set` function is chainable. | ||
var logger = require('ain2') | ||
.set('node-test-app', 'daemon', 'devhost', 3000); | ||
.set({tag: 'node-test-app', facility: 'daemon', hostname: 'devhost', port: 3000}); | ||
logger.warn('some warning'); | ||
@@ -66,5 +68,11 @@ | ||
`set` function takes three arguments: `tag`, `facility` and `hostname`. All | ||
of these are optional. | ||
`set` function takes one argument, a configuration object which can contain the following keys: | ||
* tag - defaults to __filename | ||
* facility - defaults to user | ||
* hostname - defaults to require('os').hostname() | ||
* port - defaults to 514 | ||
* transport - defaults to 'UDP', can also be 'file' | ||
All of these are optional. If you provide a `hostname` transport is automatically set to UDP | ||
`tag` and `hostname` arguments is just *RFC 3164* `TAG` and `HOSTNAME` of | ||
@@ -98,9 +106,9 @@ your messages. | ||
logger.set('node-test-app', 3); | ||
logger.set('node-test-app', 'daemon'); | ||
logger.set({tag: 'node-test-app', facility: 3}); | ||
logger.set({tag: 'node-test-app', facility: 'daemon'}); | ||
Also you can set `TAG`, `Facility` and `HOSTNAME` separatelly by `setTag`, | ||
`setFacility` and `setHostname` functions. All of them is chainable too. | ||
Also you can set `TAG`, `Facility`, `HOSTNAME`, `PORT`, and `transport` separately by `setTag`, | ||
`setFacility`, `setHostname`, `setPort` and `setTransport` functions. All of them are chainable too. | ||
You can get all destinations by theese properties: | ||
You can get all destinations by these properties: | ||
@@ -110,2 +118,3 @@ * `tag` TAG | ||
* `hostname` HOSTNAME | ||
* `port` PORT | ||
@@ -112,0 +121,0 @@ ## Logging |
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
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
17303
305
170