logger.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. const coreClass = require("../core");
  3. const config = require('config');
  4. const fs = require('fs');
  5. const twoDigits = (num) => {
  6. return (num < 10) ? '0' + num : num;
  7. };
  8. const getTime = () => {
  9. let time = new Date();
  10. return {
  11. year: time.getFullYear(),
  12. month: time.getMonth() + 1,
  13. day: time.getDate(),
  14. hour: time.getHours(),
  15. minute: time.getMinutes(),
  16. second: time.getSeconds()
  17. }
  18. };
  19. const getTimeFormatted = () => {
  20. let time = getTime();
  21. return `${time.year}-${twoDigits(time.month)}-${twoDigits(time.day)} ${twoDigits(time.hour)}:${twoDigits(time.minute)}:${twoDigits(time.second)}`;
  22. }
  23. module.exports = class extends coreClass {
  24. initialize() {
  25. return new Promise((resolve, reject) => {
  26. this.setStage(1);
  27. this.configDirectory = `${__dirname}/../../log`;
  28. if (!config.isDocker && !fs.existsSync(`${this.configDirectory}`))
  29. fs.mkdirSync(this.configDirectory);
  30. let time = getTimeFormatted();
  31. this.logCbs = [];
  32. this.colors = {
  33. Reset: "\x1b[0m",
  34. Bright: "\x1b[1m",
  35. Dim: "\x1b[2m",
  36. Underscore: "\x1b[4m",
  37. Blink: "\x1b[5m",
  38. Reverse: "\x1b[7m",
  39. Hidden: "\x1b[8m",
  40. FgBlack: "\x1b[30m",
  41. FgRed: "\x1b[31m",
  42. FgGreen: "\x1b[32m",
  43. FgYellow: "\x1b[33m",
  44. FgBlue: "\x1b[34m",
  45. FgMagenta: "\x1b[35m",
  46. FgCyan: "\x1b[36m",
  47. FgWhite: "\x1b[37m",
  48. BgBlack: "\x1b[40m",
  49. BgRed: "\x1b[41m",
  50. BgGreen: "\x1b[42m",
  51. BgYellow: "\x1b[43m",
  52. BgBlue: "\x1b[44m",
  53. BgMagenta: "\x1b[45m",
  54. BgCyan: "\x1b[46m",
  55. BgWhite: "\x1b[47m"
  56. };
  57. fs.appendFile(this.configDirectory + '/all.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  58. fs.appendFile(this.configDirectory + '/success.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  59. fs.appendFile(this.configDirectory + '/error.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  60. fs.appendFile(this.configDirectory + '/info.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  61. fs.appendFile(this.configDirectory + '/debugStation.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  62. for(let i = 0; i < this.reservedLines; i++) {
  63. process.stdout.write("\n");
  64. }
  65. resolve();
  66. });
  67. }
  68. async success(type, text, display = true) {
  69. try { await this._validateHook(); } catch { return; }
  70. const time = getTimeFormatted();
  71. const message = `${time} SUCCESS - ${type} - ${text}`;
  72. this.writeFile('all.log', message);
  73. this.writeFile('success.log', message);
  74. if (display) this.log(this.colors.FgGreen, message);
  75. }
  76. async error(type, text, display = true) {
  77. try { await this._validateHook(); } catch { return; }
  78. const time = getTimeFormatted();
  79. const message = `${time} ERROR - ${type} - ${text}`;
  80. this.writeFile('all.log', message);
  81. this.writeFile('error.log', message);
  82. if (display) this.log(this.colors.FgRed, message);
  83. }
  84. async info(type, text, display = true) {
  85. try { await this._validateHook(); } catch { return; }
  86. const time = getTimeFormatted();
  87. const message = `${time} INFO - ${type} - ${text}`;
  88. this.writeFile('all.log', message);
  89. this.writeFile('info.log', message);
  90. if (display) this.log(this.colors.FgCyan, message);
  91. }
  92. async debug(text, display = true) {
  93. try { await this._validateHook(); } catch { return; }
  94. const time = getTimeFormatted();
  95. const message = `${time} DEBUG - ${text}`;
  96. if (display) this.log(this.colors.FgMagenta, message);
  97. }
  98. async stationIssue(text, display = false) {
  99. try { await this._validateHook(); } catch { return; }
  100. const time = getTimeFormatted();
  101. const message = `${time} DEBUG_STATION - ${text}`;
  102. this.writeFile('debugStation.log', message);
  103. if (display) this.log(this.colors.FgMagenta, message);
  104. }
  105. log(color, message) {
  106. this.logCbs.push(() => {
  107. this.logCbs.shift();
  108. this.logActive = true;
  109. const rows = process.stdout.rows;
  110. const columns = process.stdout.columns;
  111. const lineNumber = rows - this.reservedLines;
  112. let lines = Math.floor(message.length / columns) + 1;
  113. process.stdout.cursorTo(0, lineNumber);
  114. process.stdout.write(`${color}${message}${this.colors.Reset}\n`);
  115. process.stdout.cursorTo(0, (rows - this.logger.reservedLines) + lines);
  116. process.stdout.clearScreenDown();
  117. process.stdout.cursorTo(0, process.stdout.rows);
  118. for(let i = 0; i < lines; i++) {
  119. process.stdout.write(`\n`);
  120. }
  121. this.moduleManager.printStatus();
  122. this.logActive = false;
  123. this.nextLog();
  124. });
  125. this.nextLog();
  126. }
  127. nextLog() {
  128. if (!this.logActive && this.logCbs.length > 0) {
  129. this.logCbs[0]();
  130. }
  131. }
  132. writeFile(fileName, message) {
  133. fs.appendFile(`${this.configDirectory}/${fileName}`, `${message}\n`, ()=>{});
  134. }
  135. }