logger.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. var util = require('util');
  2. var Settings = {
  3. log_timestamp : false,
  4. colors : true,
  5. enabled : true
  6. };
  7. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
  8. 'Oct', 'Nov', 'Dec'];
  9. function getDate() {
  10. var now = new Date();
  11. return [now.toLocaleDateString(), now.toLocaleTimeString()].join(' ');
  12. }
  13. function pad(n) {
  14. return n < 10 ? '0' + n.toString(10) : n.toString(10);
  15. }
  16. // 26 Feb 16:19:34
  17. function timestamp() {
  18. var d = new Date();
  19. var time = [pad(d.getHours()),
  20. pad(d.getMinutes()),
  21. pad(d.getSeconds())].join(':');
  22. return [d.getDate(), months[d.getMonth()], time].join(' ');
  23. }
  24. function getPrefix(tag, severity) {
  25. var levels = ['EMERG', 'ALERT', 'CRITICAL', 'ERROR', 'WARNING', 'NOTICE', 'INFO', 'DEBUG'];
  26. return tag + ' ' + levels[severity] +' - ';
  27. }
  28. function ConsoleColor() {
  29. var self = this;
  30. var mappings = {
  31. blue : '0;34',
  32. light_blue : '1;34'
  33. };
  34. this.background = new Background();
  35. this.foreground_colors = {};
  36. this.foreground_colors['black'] = '0;30';
  37. this.foreground_colors['dark_gray'] = '1;30';
  38. this.foreground_colors['blue'] = '0;34';
  39. this.foreground_colors['light_blue'] = '1;34';
  40. this.foreground_colors['green'] = '0;32';
  41. this.foreground_colors['light_green'] = '1;32';
  42. this.foreground_colors['cyan'] = '0;36';
  43. this.foreground_colors['light_cyan'] = '1;36';
  44. this.foreground_colors['red'] = '0;31';
  45. this.foreground_colors['light_red'] = '1;31';
  46. this.foreground_colors['purple'] = '0;35';
  47. this.foreground_colors['light_purple'] = '1;35';
  48. this.foreground_colors['brown'] = '0;33';
  49. this.foreground_colors['yellow'] = '1;33';
  50. this.foreground_colors['light_gray'] = '0;37';
  51. this.foreground_colors['white'] = '1;37';
  52. this.foreground_colors['stack_trace'] = '0;90';
  53. this.background_colors = {};
  54. this.background_colors['black'] = '40';
  55. this.background_colors['red'] = '41';
  56. this.background_colors['green'] = '42';
  57. this.background_colors['yellow'] = '43';
  58. this.background_colors['blue'] = '44';
  59. this.background_colors['magenta'] = '45';
  60. this.background_colors['cyan'] = '46';
  61. this.background_colors['light_gray'] = '47';
  62. Object.keys(this.foreground_colors).forEach(function(k) {
  63. ConsoleColor.prototype[k.toLowerCase()] = function Foreground(text, background) {
  64. var string = '\033[' + self.foreground_colors[k.toLowerCase()] + 'm';
  65. if (background !== undefined) {
  66. string += background();
  67. }
  68. string += text + '\033[0m';
  69. return string;
  70. };
  71. });
  72. Object.keys(this.background_colors).forEach(function(k) {
  73. Background.prototype[k.toLowerCase()] = function Background(text) {
  74. return '\033[' + self.background_colors[k.toLowerCase()] + 'm';
  75. };
  76. });
  77. return this;
  78. }
  79. function Background() { return this; }
  80. var colors = new ConsoleColor();
  81. function logObject(obj) {
  82. console.log(util.inspect(obj, {
  83. showHidden : false,
  84. depth : 3,
  85. colors : Settings.colors
  86. }));
  87. }
  88. function logTimestamp() {
  89. if (Settings.log_timestamp) {
  90. return colors.white(timestamp()) + ' ';
  91. }
  92. return '';
  93. }
  94. function logMessage(type, message, args) {
  95. if (!message || !Settings.enabled) {
  96. return;
  97. }
  98. var messageStr = '';
  99. var timestamp = logTimestamp();
  100. switch (type) {
  101. case 'ERROR':
  102. messageStr = colors.yellow(type, colors.background.dark_gray) +' '+
  103. timestamp + colors.light_green(message);
  104. break;
  105. case 'INFO':
  106. messageStr = colors.light_purple(type, colors.background.black) +' '+
  107. timestamp + colors.light_cyan(message);
  108. break;
  109. case 'LOG':
  110. messageStr = colors.white(type+' ', colors.background.black) +' '+
  111. timestamp + colors.white(message);
  112. break;
  113. case 'WARN':
  114. messageStr = colors.light_green(type, colors.background.black) +' '+
  115. timestamp + colors.light_green(message);
  116. break;
  117. }
  118. process.stdout.write(messageStr);
  119. if (args.length > 0) {
  120. var inlineArgs = [];
  121. args.forEach(function(item) {
  122. if (Object.prototype.toString.call(item) === '[object Object]' && Object.keys(item).length > 0) {
  123. if (inlineArgs.length) {
  124. console.log.apply(console, inlineArgs);
  125. inlineArgs = [];
  126. }
  127. logObject(item);
  128. } else {
  129. inlineArgs.push(item);
  130. }
  131. });
  132. if (inlineArgs.length) {
  133. process.stdout.write(' ');
  134. console.log.apply(console, inlineArgs);
  135. inlineArgs = [];
  136. }
  137. } else {
  138. process.stdout.write('\n');
  139. }
  140. }
  141. exports.info = function(message) {
  142. var args = Array.prototype.slice.call(arguments, 1);
  143. logMessage('INFO', message, args);
  144. };
  145. exports.log = function(message) {
  146. var args = Array.prototype.slice.call(arguments, 1);
  147. logMessage('LOG', message, args);
  148. };
  149. exports.warn = function(message) {
  150. var args = Array.prototype.slice.call(arguments, 1);
  151. logMessage('WARN', message, args);
  152. };
  153. exports.error = function(message) {
  154. var args = Array.prototype.slice.call(arguments, 1);
  155. logMessage('ERROR', message, args);
  156. };
  157. exports.disableColors = function () {
  158. Settings.colors = false;
  159. Object.keys(ConsoleColor.prototype).forEach(function (color) {
  160. ConsoleColor.prototype[color] = function (text) {
  161. return text;
  162. };
  163. });
  164. };
  165. exports.disable = function() {
  166. Settings.enabled = false;
  167. };
  168. exports.enable = function() {
  169. Settings.enabled = true;
  170. };
  171. exports.isEnabled = function() {
  172. return Settings.enabled;
  173. };
  174. exports.colors = colors;