log.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*!
  2. * Log.js
  3. * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var fmt = require('util').format;
  10. var EventEmitter = require('events').EventEmitter;
  11. /**
  12. * Initialize a `Loggeer` with the given log `level` defaulting
  13. * to __DEBUG__ and `stream` defaulting to _stdout_.
  14. *
  15. * @param {Number} level
  16. * @param {Object} stream
  17. * @api public
  18. */
  19. var Log = exports = module.exports = function Log(level, stream){
  20. if ('string' == typeof level) level = exports[level.toUpperCase()];
  21. this.level = level || exports.DEBUG;
  22. this.stream = stream || process.stdout;
  23. if (this.stream.readable) this.read();
  24. };
  25. /**
  26. * System is unusable.
  27. *
  28. * @type Number
  29. */
  30. exports.EMERGENCY = 0;
  31. /**
  32. * Action must be taken immediately.
  33. *
  34. * @type Number
  35. */
  36. exports.ALERT = 1;
  37. /**
  38. * Critical condition.
  39. *
  40. * @type Number
  41. */
  42. exports.CRITICAL = 2;
  43. /**
  44. * Error condition.
  45. *
  46. * @type Number
  47. */
  48. exports.ERROR = 3;
  49. /**
  50. * Warning condition.
  51. *
  52. * @type Number
  53. */
  54. exports.WARNING = 4;
  55. /**
  56. * Normal but significant condition.
  57. *
  58. * @type Number
  59. */
  60. exports.NOTICE = 5;
  61. /**
  62. * Purely informational message.
  63. *
  64. * @type Number
  65. */
  66. exports.INFO = 6;
  67. /**
  68. * Application debug messages.
  69. *
  70. * @type Number
  71. */
  72. exports.DEBUG = 7;
  73. /**
  74. * prototype.
  75. */
  76. Log.prototype = {
  77. /**
  78. * Start emitting "line" events.
  79. *
  80. * @api public
  81. */
  82. read: function(){
  83. var buf = ''
  84. , self = this
  85. , stream = this.stream;
  86. stream.setEncoding('utf8');
  87. stream.on('data', function(chunk){
  88. buf += chunk;
  89. if ('\n' != buf[buf.length - 1]) return;
  90. buf.split('\n').map(function(line){
  91. if (!line.length) return;
  92. try {
  93. var captures = line.match(/^\[([^\]]+)\] (\w+) (.*)/);
  94. var obj = {
  95. date: new Date(captures[1])
  96. , level: exports[captures[2]]
  97. , levelString: captures[2]
  98. , msg: captures[3]
  99. };
  100. self.emit('line', obj);
  101. } catch (err) {
  102. // Ignore
  103. }
  104. });
  105. buf = '';
  106. });
  107. stream.on('end', function(){
  108. self.emit('end');
  109. });
  110. },
  111. /**
  112. * Log output message.
  113. *
  114. * @param {String} levelStr
  115. * @param {Array} args
  116. * @api private
  117. */
  118. log: function(levelStr, args) {
  119. if (exports[levelStr] <= this.level) {
  120. var msg = fmt.apply(null, args);
  121. this.stream.write(
  122. '[' + new Date + ']'
  123. + ' ' + levelStr
  124. + ' ' + msg
  125. + '\n'
  126. );
  127. }
  128. },
  129. /**
  130. * Log emergency `msg`.
  131. *
  132. * @param {String} msg
  133. * @api public
  134. */
  135. emergency: function(msg){
  136. this.log('EMERGENCY', arguments);
  137. },
  138. /**
  139. * Log alert `msg`.
  140. *
  141. * @param {String} msg
  142. * @api public
  143. */
  144. alert: function(msg){
  145. this.log('ALERT', arguments);
  146. },
  147. /**
  148. * Log critical `msg`.
  149. *
  150. * @param {String} msg
  151. * @api public
  152. */
  153. critical: function(msg){
  154. this.log('CRITICAL', arguments);
  155. },
  156. /**
  157. * Log error `msg`.
  158. *
  159. * @param {String} msg
  160. * @api public
  161. */
  162. error: function(msg){
  163. this.log('ERROR', arguments);
  164. },
  165. /**
  166. * Log warning `msg`.
  167. *
  168. * @param {String} msg
  169. * @api public
  170. */
  171. warning: function(msg){
  172. this.log('WARNING', arguments);
  173. },
  174. /**
  175. * Log notice `msg`.
  176. *
  177. * @param {String} msg
  178. * @api public
  179. */
  180. notice: function(msg){
  181. this.log('NOTICE', arguments);
  182. },
  183. /**
  184. * Log info `msg`.
  185. *
  186. * @param {String} msg
  187. * @api public
  188. */
  189. info: function(msg){
  190. this.log('INFO', arguments);
  191. },
  192. /**
  193. * Log debug `msg`.
  194. *
  195. * @param {String} msg
  196. * @api public
  197. */
  198. debug: function(msg){
  199. this.log('DEBUG', arguments);
  200. }
  201. };
  202. /**
  203. * Inherit from `EventEmitter`.
  204. */
  205. Log.prototype.__proto__ = EventEmitter.prototype;