123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /*!
- * Log.js
- * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
- * MIT Licensed
- */
- /**
- * Module dependencies.
- */
- var fmt = require('util').format;
- var EventEmitter = require('events').EventEmitter;
- /**
- * Initialize a `Loggeer` with the given log `level` defaulting
- * to __DEBUG__ and `stream` defaulting to _stdout_.
- *
- * @param {Number} level
- * @param {Object} stream
- * @api public
- */
- var Log = exports = module.exports = function Log(level, stream){
- if ('string' == typeof level) level = exports[level.toUpperCase()];
- this.level = level || exports.DEBUG;
- this.stream = stream || process.stdout;
- if (this.stream.readable) this.read();
- };
- /**
- * System is unusable.
- *
- * @type Number
- */
- exports.EMERGENCY = 0;
- /**
- * Action must be taken immediately.
- *
- * @type Number
- */
- exports.ALERT = 1;
- /**
- * Critical condition.
- *
- * @type Number
- */
- exports.CRITICAL = 2;
- /**
- * Error condition.
- *
- * @type Number
- */
- exports.ERROR = 3;
- /**
- * Warning condition.
- *
- * @type Number
- */
- exports.WARNING = 4;
- /**
- * Normal but significant condition.
- *
- * @type Number
- */
- exports.NOTICE = 5;
- /**
- * Purely informational message.
- *
- * @type Number
- */
- exports.INFO = 6;
- /**
- * Application debug messages.
- *
- * @type Number
- */
- exports.DEBUG = 7;
- /**
- * prototype.
- */
- Log.prototype = {
- /**
- * Start emitting "line" events.
- *
- * @api public
- */
- read: function(){
- var buf = ''
- , self = this
- , stream = this.stream;
- stream.setEncoding('utf8');
- stream.on('data', function(chunk){
- buf += chunk;
- if ('\n' != buf[buf.length - 1]) return;
- buf.split('\n').map(function(line){
- if (!line.length) return;
- try {
- var captures = line.match(/^\[([^\]]+)\] (\w+) (.*)/);
- var obj = {
- date: new Date(captures[1])
- , level: exports[captures[2]]
- , levelString: captures[2]
- , msg: captures[3]
- };
- self.emit('line', obj);
- } catch (err) {
- // Ignore
- }
- });
- buf = '';
- });
- stream.on('end', function(){
- self.emit('end');
- });
- },
- /**
- * Log output message.
- *
- * @param {String} levelStr
- * @param {Array} args
- * @api private
- */
- log: function(levelStr, args) {
- if (exports[levelStr] <= this.level) {
- var msg = fmt.apply(null, args);
- this.stream.write(
- '[' + new Date + ']'
- + ' ' + levelStr
- + ' ' + msg
- + '\n'
- );
- }
- },
- /**
- * Log emergency `msg`.
- *
- * @param {String} msg
- * @api public
- */
- emergency: function(msg){
- this.log('EMERGENCY', arguments);
- },
- /**
- * Log alert `msg`.
- *
- * @param {String} msg
- * @api public
- */
- alert: function(msg){
- this.log('ALERT', arguments);
- },
- /**
- * Log critical `msg`.
- *
- * @param {String} msg
- * @api public
- */
- critical: function(msg){
- this.log('CRITICAL', arguments);
- },
- /**
- * Log error `msg`.
- *
- * @param {String} msg
- * @api public
- */
- error: function(msg){
- this.log('ERROR', arguments);
- },
- /**
- * Log warning `msg`.
- *
- * @param {String} msg
- * @api public
- */
- warning: function(msg){
- this.log('WARNING', arguments);
- },
- /**
- * Log notice `msg`.
- *
- * @param {String} msg
- * @api public
- */
- notice: function(msg){
- this.log('NOTICE', arguments);
- },
- /**
- * Log info `msg`.
- *
- * @param {String} msg
- * @api public
- */
- info: function(msg){
- this.log('INFO', arguments);
- },
- /**
- * Log debug `msg`.
- *
- * @param {String} msg
- * @api public
- */
- debug: function(msg){
- this.log('DEBUG', arguments);
- }
- };
- /**
- * Inherit from `EventEmitter`.
- */
- Log.prototype.__proto__ = EventEmitter.prototype;
|