log.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. (function () {
  3. var util = require('util');
  4. // log level
  5. var LEVEL = {
  6. ALL:Infinity,
  7. INFO:3,
  8. WARN:2,
  9. ERROR:1,
  10. NONE:-Infinity
  11. };
  12. // log color
  13. var COLOR = {
  14. RESET:'\u001b[0m',
  15. INFO:'\u001b[32m', // green
  16. WARN:'\u001b[33m', // yellow
  17. ERROR:'\u001b[31m' // red
  18. };
  19. // global log level
  20. var globalLevel = LEVEL.ALL;
  21. // whether log output should be colored
  22. var coloredOutput = true;
  23. function setLevel(level) {
  24. globalLevel = level;
  25. }
  26. function setColoredOutput(bool) {
  27. coloredOutput = bool;
  28. }
  29. function info() {
  30. if (LEVEL.INFO <= globalLevel) {
  31. log(LEVEL.INFO, util.format.apply(this, arguments));
  32. }
  33. }
  34. function warn() {
  35. if (LEVEL.WARN <= globalLevel) {
  36. log(LEVEL.WARN, util.format.apply(this, arguments));
  37. }
  38. }
  39. function error() {
  40. if (LEVEL.ERROR <= globalLevel) {
  41. log(LEVEL.ERROR, util.format.apply(this, arguments));
  42. }
  43. }
  44. function newPrepareStackTrace(error, structuredStack) {
  45. return structuredStack;
  46. }
  47. // must not be called directly due to stack trace
  48. function log(level, message) {
  49. // get call stack and find the caller
  50. var oldPrepareStackTrace = Error.prepareStackTrace;
  51. Error.prepareStackTrace = newPrepareStackTrace;
  52. var structuredStack = new Error().stack;
  53. Error.prepareStackTrace = oldPrepareStackTrace;
  54. var caller = structuredStack[2];
  55. var lineSep = process.platform == 'win32' ? '\\' : '/';
  56. var fileNameSplited = caller.getFileName().split(lineSep);
  57. var fileName = fileNameSplited[fileNameSplited.length - 1];
  58. var lineNumber = caller.getLineNumber();
  59. var columnNumber = caller.getColumnNumber();
  60. // function name may be empty if it is a global call
  61. // var functionName = caller.getFunctionName();
  62. var levelString;
  63. switch (level) {
  64. case LEVEL.INFO:
  65. levelString = '[INFO]';
  66. break;
  67. case LEVEL.WARN:
  68. levelString = '[WARN]';
  69. break;
  70. case LEVEL.ERROR:
  71. levelString = '[ERROR]';
  72. break;
  73. default:
  74. levelString = '[]';
  75. break;
  76. }
  77. var output = util.format('%s %s(%d,%d): %s',
  78. levelString, fileName, lineNumber, columnNumber, message
  79. );
  80. if (!coloredOutput) {
  81. process.stdout.write(output + '\n');
  82. } else {
  83. switch (level) {
  84. case LEVEL.INFO:
  85. process.stdout.write(COLOR.INFO + output + COLOR.RESET + '\n');
  86. break;
  87. case LEVEL.WARN:
  88. process.stdout.write(COLOR.WARN + output + COLOR.RESET + '\n');
  89. break;
  90. case LEVEL.ERROR:
  91. process.stdout.write(COLOR.ERROR + output + COLOR.RESET + '\n');
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. }
  98. module.exports = {
  99. info:info,
  100. warn:warn,
  101. error:error,
  102. LEVEL:LEVEL,
  103. setLevel:setLevel,
  104. setColoredOutput:setColoredOutput
  105. };
  106. }());