Comparing version 1.4.0 to 1.5.0
# CHANGELOG | ||
## v1.5.0 | ||
* Bubble up callbacks from udp and unix socket up to the caller | ||
## v1.4.0 | ||
@@ -5,0 +8,0 @@ |
104
index.js
@@ -87,3 +87,3 @@ var dgram = require('dgram'); | ||
var Transport = { | ||
UDP: function(message, severity) { | ||
UDP: function(message, severity, callback) { | ||
var self = this; | ||
@@ -97,2 +97,6 @@ var syslogMessage = this.composerFunction(message, severity); | ||
function(err, bytes) { | ||
if(callback){ | ||
console.log(callback); | ||
callback(err, bytes); | ||
} | ||
self._logError(err, bytes); | ||
@@ -103,3 +107,3 @@ releaseSocket(); | ||
}, | ||
unix_dgram: function(message, severity){ | ||
unix_dgram: function(message, severity, callback){ | ||
var self = this; | ||
@@ -128,2 +132,5 @@ var preambleBuffer = self.composerFunction('', severity); | ||
function(err, bytes){ | ||
if(callback){ | ||
callback(err, bytes); | ||
} | ||
self._logError(err, bytes); | ||
@@ -171,14 +178,16 @@ releaseSocket(); | ||
/** | ||
* Just copy from node.js console | ||
* @param f | ||
* Internal message formatting method | ||
* @param args [format, arg1, arg2, ...] | ||
* @returns | ||
*/ | ||
function format(f) { | ||
function format(args) { | ||
var util = require('util'), | ||
i = 0; | ||
i = 0, | ||
f = args[0]; | ||
// inspect the object if no format string was given as the first argument | ||
if (typeof f !== 'string') { | ||
var objects = []; | ||
for (i = 0; i < arguments.length; i++) { | ||
objects.push(util.inspect(arguments[i])); | ||
for (i = 0; i < args.length; i++) { | ||
objects.push(util.inspect(args[i])); | ||
} | ||
@@ -190,3 +199,2 @@ return objects.join(' '); | ||
i = 1; | ||
var args = arguments; | ||
var str = String(f).replace(formatRegExp, function(x) { | ||
@@ -327,4 +335,4 @@ switch (x) { | ||
*/ | ||
SysLogger.prototype._send = function(message, severity) { | ||
this.transport(message, severity) ; | ||
SysLogger.prototype._send = function(message, severity, callback) { | ||
this.transport(message, severity, callback) ; | ||
}; | ||
@@ -337,10 +345,24 @@ | ||
*/ | ||
SysLogger.prototype.send = function(message, severity) { | ||
severity = severity || Severity.notice; | ||
SysLogger.prototype.send = function(message, severity, callback) { | ||
if (typeof severity == 'string'){ | ||
severity = Severity[severity]; | ||
} | ||
this._send(message, severity); | ||
this._send(message, severity, callback); | ||
}; | ||
SysLogger.prototype.formatAndSend = function(argumentsObject, severity) { | ||
var args = Array.prototype.slice.call(argumentsObject); | ||
var callback = undefined; | ||
var message = undefined; | ||
if(Object.prototype.toString.call(args[args.length - 1]) == "[object Function]"){ | ||
callback = args.pop(); | ||
} | ||
this._send(format(args), severity, callback); | ||
}; | ||
/** | ||
@@ -350,3 +372,3 @@ * Send log message with notice severity. | ||
SysLogger.prototype.log = function() { | ||
this._send(format.apply(this, arguments), Severity.notice); | ||
this.formatAndSend(arguments, Severity.notice); | ||
}; | ||
@@ -357,3 +379,3 @@ /** | ||
SysLogger.prototype.info = function() { | ||
this._send(format.apply(this, arguments), Severity.info); | ||
this.formatAndSend(arguments, Severity.info); | ||
}; | ||
@@ -364,3 +386,3 @@ /** | ||
SysLogger.prototype.warn = function() { | ||
this._send(format.apply(this, arguments), Severity.warn); | ||
this.formatAndSend(arguments, Severity.warn); | ||
}; | ||
@@ -371,3 +393,3 @@ /** | ||
SysLogger.prototype.error = function() { | ||
this._send(format.apply(this, arguments), Severity.err); | ||
this.formatAndSend(arguments, Severity.err); | ||
}; | ||
@@ -378,19 +400,9 @@ /** | ||
SysLogger.prototype.debug = function() { | ||
this._send(format.apply(this, arguments), Severity.debug); | ||
this.formatAndSend(arguments, Severity.debug); | ||
}; | ||
/** | ||
* Compose syslog message | ||
*/ | ||
SysLogger.prototype.composeSyslogMessage = function(message, severity) { | ||
return new Buffer('<' + (this.facility * 8 + severity) + '>' + | ||
this.getDate() + ' ' + this.hostname + ' ' + | ||
this.tag + '[' + process.pid + ']:' + message); | ||
} | ||
/** | ||
* Log object with `util.inspect` with notice severity | ||
*/ | ||
SysLogger.prototype.dir = function(object) { | ||
SysLogger.prototype.dir = function(object, callback) { | ||
var util = require('util'); | ||
@@ -400,11 +412,3 @@ this._send(util.inspect(object) + '\n', Severity.notice); | ||
SysLogger.prototype.time = function(label) { | ||
this._times[label] = Date.now(); | ||
}; | ||
SysLogger.prototype.timeEnd = function(label) { | ||
var duration = Date.now() - this._times[label]; | ||
this.log('%s: %dms', label, duration); | ||
}; | ||
SysLogger.prototype.trace = function(label) { | ||
SysLogger.prototype.trace = function(label, callback) { | ||
var err = new Error(); | ||
@@ -414,3 +418,3 @@ err.name = 'Trace'; | ||
Error.captureStackTrace(err, arguments.callee); | ||
this.error(err.stack); | ||
this.error(err.stack, callback); | ||
}; | ||
@@ -421,7 +425,25 @@ | ||
var arr = Array.prototype.slice.call(arguments, 1); | ||
this._send(format.apply(this, arr), Severity.err); | ||
this._send(format(arr), Severity.err); | ||
} | ||
}; | ||
/** | ||
* Compose syslog message | ||
*/ | ||
SysLogger.prototype.composeSyslogMessage = function(message, severity) { | ||
return new Buffer('<' + (this.facility * 8 + severity) + '>' + | ||
this.getDate() + ' ' + this.hostname + ' ' + | ||
this.tag + '[' + process.pid + ']:' + message); | ||
} | ||
SysLogger.prototype.time = function(label) { | ||
this._times[label] = Date.now(); | ||
}; | ||
SysLogger.prototype.timeEnd = function(label) { | ||
var duration = Date.now() - this._times[label]; | ||
this.log('%s: %dms', label, duration); | ||
}; | ||
/** | ||
* Get current date in syslog format. | ||
@@ -428,0 +450,0 @@ * @returns {String} |
{ | ||
"name": "ain2", | ||
"description": "Syslog logging for node.js. Continuation of ain", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"main": "./index", | ||
@@ -6,0 +6,0 @@ "author": "Alexander Dorofeev <aka.spin@gmail.com>", |
# ain* | ||
Brain-free [syslog](http://en.wikipedia.org/wiki/Syslog)** logging for | ||
Brain-free [syslog](http://en.wikipedia.org/wiki/Syslog)** logging for | ||
[node.js](http://nodejs.org). | ||
*Ain* is written with full compatibility with *node.js* `console` module. It | ||
implements all `console` functions and formatting. Also *ain* supports UTF-8 | ||
*Ain* is written with full compatibility with *node.js* `console` module. It | ||
implements all `console` functions and formatting. Also *ain* supports UTF-8 | ||
(tested on Debian Testing/Sid). | ||
*Ain* can send messages by UDP to `127.0.0.1:514` or to the a unix socket; | ||
*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. Unix socket support is possible if [unix-dgram](https://npmjs.org/package/unix-dgram) can be built and installed. | ||
@@ -16,3 +16,3 @@ | ||
**All examples tested under Ubuntu `rsyslog`. On other operating | ||
**All examples tested under Ubuntu `rsyslog`. On other operating | ||
systems and logging daemons settings and paths may differ. | ||
@@ -22,3 +22,3 @@ | ||
You can install *ain* as usual - by copy the "ain" directory in your | ||
You can install *ain* as usual - by copy the "ain" directory in your | ||
`~/.node_modules` or via *npm* | ||
@@ -30,3 +30,3 @@ | ||
Usage of *ain* is very similar to the *node.js* console. The following example | ||
Usage of *ain* is very similar to the *node.js* console. The following example | ||
demonstrates the replacement of the console: | ||
@@ -36,7 +36,7 @@ | ||
var console = new SysLogger(); | ||
console.log('notice: %d', Date.now()); | ||
console.info('info'); | ||
console.error('error'); | ||
After launch in `/var/log/user` you can see the following: | ||
@@ -62,3 +62,3 @@ | ||
require('ain2').getInstance(); | ||
If you use this, please be beware of this: | ||
@@ -68,3 +68,3 @@ | ||
=> true | ||
As opposed to: | ||
@@ -75,3 +75,3 @@ | ||
=> false | ||
## Changing destinations | ||
@@ -94,7 +94,7 @@ | ||
logger.warn('some warning'); | ||
... and in `/var/log/daemon.log`: | ||
Dec 5 07:08:58 devhost node-test-app[10045]: some warning | ||
The `set` function takes one argument, a configuration object which can contain the following keys: | ||
@@ -111,6 +111,6 @@ * tag - defaults to __filename | ||
`tag` and `hostname` arguments is just *RFC 3164* `TAG` and `HOSTNAME` of | ||
`tag` and `hostname` arguments is just *RFC 3164* `TAG` and `HOSTNAME` of | ||
your messages. | ||
`facility` is little more than just name. Refer to *Section 4.1.1* of | ||
`facility` is little more than just name. Refer to *Section 4.1.1* of | ||
[RFC 3164](http://www.faqs.org/rfcs/rfc3164.html) it can be: | ||
@@ -142,4 +142,4 @@ | ||
logger.set({tag: 'node-test-app', facility: 'daemon'}); | ||
Also you can set `TAG`, `Facility`, `HOSTNAME`, `PORT`, and `transport` separately by `setTag`, | ||
Also you can set `TAG`, `Facility`, `HOSTNAME`, `PORT`, and `transport` separately by `setTag`, | ||
`setFacility`, `setHostname`, `setPort`, `setTransport` and `setMessageComposer` functions. All of them are chainable too. | ||
@@ -154,2 +154,19 @@ | ||
## Callbacks | ||
Ain provides an optional callback after a message has been sent to the socket (udp and unix socket). | ||
The callback is passed up unaltered from [node.](http://nodejs.org/api/dgram.html#dgram_socket_send_buf_offset_length_port_address_callback) | ||
Because ain supports a simplified printf format, the callback has to be the last parameter. | ||
var SysLogger = require('ain2'); | ||
var console = new SysLogger(); | ||
console.info('info', function(err, bytes){ | ||
// callback received | ||
}); | ||
console.log('notice: %d', Date.now(), function(err, bytes){ | ||
// callback received | ||
}); | ||
## Custom message composer | ||
@@ -165,3 +182,3 @@ | ||
//The default implementation looks this: | ||
@@ -172,3 +189,3 @@ | ||
return new Buffer('<' + (this.facility * 8 + severity) + '>' + | ||
this.getDate() + ' ' + this.hostname + ' ' + | ||
this.getDate() + ' ' + this.hostname + ' ' + | ||
this.tag + '[' + process.pid + ']:' + message); | ||
@@ -179,3 +196,3 @@ } | ||
As noticed before *ain* implements all `console` functions. Severity level is | ||
As noticed before *ain* implements all `console` functions. Severity level is | ||
referenced to [RFC 3164](http://www.faqs.org/rfcs/rfc3164.html): | ||
@@ -194,4 +211,4 @@ | ||
*Ain* `console`-like functions behaviour is fully compatible to *node.js* and | ||
logs messages with different severity levels: | ||
*Ain* `console`-like functions behaviour is fully compatible to *node.js* and | ||
logs messages with different severity levels: | ||
@@ -210,4 +227,4 @@ * `log` - notice (5) | ||
logger.send('message', 'alert'); | ||
The `send` function takes two arguments: message and optional severity level. By | ||
The `send` function takes two arguments: message and optional severity level. By | ||
default, the severity level is *notice*. | ||
@@ -214,0 +231,0 @@ |
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
34561
760
220