You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.2 KiB

  1. /*jshint esversion: 6 */
  2. var Store = require("./storage/file");
  3. var Logger = module.exports;
  4. /* Every line in the log should contain:
  5. * timestamp
  6. * public key of initiator
  7. * the action
  8. * the event's tag
  9. */
  10. var messageTemplate = function (type, time, tag, info) {
  11. return JSON.stringify([type.toUpperCase(), time, tag, info]);
  12. };
  13. var noop = function () {};
  14. var write = function (ctx, content) {
  15. if (!ctx.store) { return; }
  16. ctx.store.log(ctx.channelName, content, noop);
  17. };
  18. // various degrees of logging
  19. const logLevels = Logger.levels = ['silly', 'verbose', 'debug', 'feedback', 'info', 'warn', 'error'];
  20. var handlers = {
  21. silly: function (ctx, time, tag, info) {
  22. console.log('[SILLY]', time, tag, info);
  23. },
  24. debug: function (ctx, time, tag, info) {
  25. console.log('[DEBUG]', time, tag, info);
  26. },
  27. verbose: function (ctx, time, tag, info) {
  28. console.log('[VERBOSE]', time, tag, info);
  29. },
  30. feedback: function (ctx, time, tag, info) {
  31. console.log('[FEEDBACK]', time, tag, info);
  32. },
  33. info: function (ctx, time, tag, info) {
  34. console.info('[INFO]', time, tag, info);
  35. },
  36. warn: function (ctx, time, tag, info) {
  37. console.warn('[WARN]', time, tag, info);
  38. },
  39. error: function (ctx, time, tag, info) {
  40. console.error('[ERROR]', time, tag, info);
  41. }
  42. };
  43. var noop = function () {};
  44. var createLogType = function (ctx, type) {
  45. if (logLevels.indexOf(type) < logLevels.indexOf(ctx.logLevel)) {
  46. return noop;
  47. }
  48. return function (tag, info) {
  49. if (ctx.shutdown) {
  50. throw new Error("Logger has been shut down!");
  51. }
  52. var time = new Date().toISOString();
  53. var content;
  54. try {
  55. content = messageTemplate(type, time, tag, info);
  56. } catch (e) {
  57. return;
  58. }
  59. if (ctx.logToStdout && typeof(handlers[type]) === 'function') {
  60. handlers[type](ctx, time, tag, info);
  61. }
  62. write(ctx, content);
  63. };
  64. };
  65. var createMethods = function (ctx) {
  66. var log = {};
  67. logLevels.forEach(function (type) {
  68. log[type] = createLogType(ctx, type);
  69. });
  70. return log;
  71. };
  72. Logger.create = function (config, cb) {
  73. if (typeof(config.logLevel) !== 'string') {
  74. config.logLevel = 'info';
  75. }
  76. var date = new Date();
  77. var launchTime = ('' + date.getUTCFullYear()).slice(-2) + date.toISOString();
  78. var ctx = {
  79. channelName: launchTime,
  80. logFeedback: Boolean(config.logFeedback),
  81. logLevel: config.logLevel,
  82. logToStdout: config.logToStdout,
  83. };
  84. if (!config.logPath) {
  85. console.log("No logPath configured. Logging to file disabled");
  86. var logger = createMethods(ctx);
  87. logger.shutdown = noop;
  88. return void cb(Object.freeze(logger));
  89. }
  90. Store.create({
  91. filePath: config.logPath,
  92. }, function (err, store) {
  93. if (err) {
  94. throw err;
  95. }
  96. ctx.store = store;
  97. var logger = createMethods(ctx);
  98. logger.shutdown = function () {
  99. delete ctx.store;
  100. ctx.shutdown = true;
  101. store.shutdown();
  102. };
  103. cb(Object.freeze(logger));
  104. });
  105. };