Skip to content
Browse files

typo

  • Loading branch information...
1 parent 1a8d27d commit fa894bbc6f0dd9df2582e8a592c0fee4dfbbd085 @akaspin committed Dec 5, 2010
Showing with 58 additions and 13 deletions.
  1. +42 −7 index.js
  2. +16 −6 readme.md
View
49 index.js
@@ -65,17 +65,26 @@ function format(f) {
return str;
}
+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 = dt.getHours();
- var minutes = dt.getMinutes();
- var seconds = dt.getSeconds();
+ var hours = leadZero(dt.getHours());
+ var minutes = leadZero(dt.getMinutes());
+ var seconds = leadZero(dt.getSeconds());
var month = dt.getMonth();
var day = dt.getDate();
+ (day < 10) && (day = ' ' + day);
var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec' ];
return months[month] + " " + day + " " + hours + ":" + minutes
@@ -88,10 +97,11 @@ function getDate() {
* @returns {SysLogger}
*/
function SysLogger() {
+ this.times = {};
}
/**
* Init function
- * @param {Facility|Number|String} facility
+ * @param {Facility|Number|String} Optional facility
* @param {String} name Optional name. By default is process.argv[1]
* @param {String} hostname Optional hostname.
*/
@@ -105,8 +115,8 @@ SysLogger.prototype.init = function(facility, name, hostname) {
};
/**
* Get new instance of SysLogger.
- * @param {Facility|Number|String} facility
* @param {String} name Optional name. By default is process.argv[1]
+ * @param {Facility|Number|String} facility
* @param {String} hostname Optional hostname.
* @returns {SysLogger}
*/
@@ -122,14 +132,16 @@ SysLogger.prototype.get = function() {
*/
SysLogger.prototype._send = function(message, severity) {
var client = dgram.createSocket('udp4');
- var message = new Buffer('<' + this.facility * 8 + severity + '>' +
+ var message = new Buffer('<' + (this.facility * 8 + severity) + '>' +
getDate() + ' ' + this.hostname + ' ' +
this.name + '[' + process.pid + ']:' + message);
+ console.log('%s', message);
+
client.send(message, 0, message.length, 514, '127.0.0.1',
function(err) {
if (err) console.error('Can\'t connect to localhost:514');
- client.close();
});
+ client.close();
};
/**
@@ -161,6 +173,29 @@ SysLogger.prototype.dir = function(object) {
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) {
+ var err = new Error;
+ err.name = 'Trace';
+ err.message = label || '';
+ Error.captureStackTrace(err, arguments.callee);
+ this.error(err.stack);
+};
+
+SysLogger.prototype.assert = function(expression) {
+ if (!expression) {
+ var arr = Array.prototype.slice.call(arguments, 1);
+ this._send(format.apply(this, arr), Severity.err);
+ }
+};
var logger = new SysLogger();
+logger.init();
module.exports = logger;
View
22 readme.md
@@ -1,19 +1,29 @@
-# ain
+# ain*
-Brain-free node.js logger for syslog.
+Brain-free [syslog](http://en.wikipedia.org/wiki/Syslog)** logging for
+[node.js](http://nodejs.org).
*Ain* 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).
+*In the Phoenician alphabet letter "ain" indicates eye.
+**All examples tested under Debian Squeeze `rsyslog`. On other operating
+systems and logging daemons settings and paths may differ.
+
## Usage
Usage of *ain* is very similar to *node.js* console. Following example
demonstrates the replacement of the console:
var console = require('ain');
- console.init(logger.LEVEL0, 'node-test-app');
- console.log('notice level by number %d', Date.now());
- console.info('info level');
-
+ console.log('notice severity by number %d', Date.now());
+ console.info('info severity');
+ console.error('error severity');
+
+After launch in `/var/log/user` you can see the following:
+
+
+
+As noticed before *ain* implements all `console` functions.

0 comments on commit fa894bb

Please sign in to comment.
Something went wrong with that request. Please try again.