logger.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. constructor(name, moduleManager) {
  25. super(name, moduleManager);
  26. this.lockdownImmune = true;
  27. }
  28. initialize() {
  29. return new Promise((resolve, reject) => {
  30. this.setStage(1);
  31. this.configDirectory = `${__dirname}/../../log`;
  32. if (!config.isDocker && !fs.existsSync(`${this.configDirectory}`))
  33. fs.mkdirSync(this.configDirectory);
  34. let time = getTimeFormatted();
  35. this.logCbs = [];
  36. this.colors = {
  37. Reset: "\x1b[0m",
  38. Bright: "\x1b[1m",
  39. Dim: "\x1b[2m",
  40. Underscore: "\x1b[4m",
  41. Blink: "\x1b[5m",
  42. Reverse: "\x1b[7m",
  43. Hidden: "\x1b[8m",
  44. FgBlack: "\x1b[30m",
  45. FgRed: "\x1b[31m",
  46. FgGreen: "\x1b[32m",
  47. FgYellow: "\x1b[33m",
  48. FgBlue: "\x1b[34m",
  49. FgMagenta: "\x1b[35m",
  50. FgCyan: "\x1b[36m",
  51. FgWhite: "\x1b[37m",
  52. BgBlack: "\x1b[40m",
  53. BgRed: "\x1b[41m",
  54. BgGreen: "\x1b[42m",
  55. BgYellow: "\x1b[43m",
  56. BgBlue: "\x1b[44m",
  57. BgMagenta: "\x1b[45m",
  58. BgCyan: "\x1b[46m",
  59. BgWhite: "\x1b[47m"
  60. };
  61. fs.appendFile(this.configDirectory + '/all.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  62. fs.appendFile(this.configDirectory + '/success.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  63. fs.appendFile(this.configDirectory + '/error.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  64. fs.appendFile(this.configDirectory + '/info.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  65. fs.appendFile(this.configDirectory + '/debugStation.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  66. if (this.moduleManager.fancyConsole) {
  67. for(let i = 0; i < this.reservedLines; i++) {
  68. process.stdout.write("\n");
  69. }
  70. }
  71. resolve();
  72. });
  73. }
  74. async success(type, text, display = true) {
  75. try { await this._validateHook(); } catch { return; }
  76. const time = getTimeFormatted();
  77. const message = `${time} SUCCESS - ${type} - ${text}`;
  78. this.writeFile('all.log', message);
  79. this.writeFile('success.log', message);
  80. if (display) this.log(this.colors.FgGreen, message);
  81. }
  82. async error(type, text, display = true) {
  83. try { await this._validateHook(); } catch { return; }
  84. const time = getTimeFormatted();
  85. const message = `${time} ERROR - ${type} - ${text}`;
  86. this.writeFile('all.log', message);
  87. this.writeFile('error.log', message);
  88. if (display) this.log(this.colors.FgRed, message);
  89. }
  90. async info(type, text, display = true) {
  91. try { await this._validateHook(); } catch { return; }
  92. const time = getTimeFormatted();
  93. const message = `${time} INFO - ${type} - ${text}`;
  94. this.writeFile('all.log', message);
  95. this.writeFile('info.log', message);
  96. if (display) this.log(this.colors.FgCyan, message);
  97. }
  98. async debug(text, display = true) {
  99. try { await this._validateHook(); } catch { return; }
  100. const time = getTimeFormatted();
  101. const message = `${time} DEBUG - ${text}`;
  102. if (display) this.log(this.colors.FgMagenta, message);
  103. }
  104. async stationIssue(text, display = false) {
  105. try { await this._validateHook(); } catch { return; }
  106. const time = getTimeFormatted();
  107. const message = `${time} DEBUG_STATION - ${text}`;
  108. this.writeFile('debugStation.log', message);
  109. if (display) this.log(this.colors.FgMagenta, message);
  110. }
  111. log(color, message) {
  112. this.logCbs.push(() => {
  113. this.logCbs.shift();
  114. this.logActive = true;
  115. if (this.moduleManager.fancyConsole) {
  116. const rows = process.stdout.rows;
  117. const columns = process.stdout.columns;
  118. const lineNumber = rows - this.reservedLines;
  119. let lines = Math.floor(message.length / columns) + 1;
  120. process.stdout.cursorTo(0, lineNumber);
  121. process.stdout.write(`${color}${message}${this.colors.Reset}\n`);
  122. process.stdout.cursorTo(0, (rows - this.logger.reservedLines) + lines);
  123. process.stdout.clearScreenDown();
  124. process.stdout.cursorTo(0, process.stdout.rows);
  125. for(let i = 0; i < lines; i++) {
  126. process.stdout.write(`\n`);
  127. }
  128. this.moduleManager.printStatus();
  129. } else console.log(`${color}${message}${this.colors.Reset}`);
  130. this.logActive = false;
  131. this.nextLog();
  132. });
  133. this.nextLog();
  134. }
  135. nextLog() {
  136. if (!this.logActive && this.logCbs.length > 0) {
  137. this.logCbs[0]();
  138. }
  139. }
  140. writeFile(fileName, message) {
  141. fs.appendFile(`${this.configDirectory}/${fileName}`, `${message}\n`, ()=>{});
  142. }
  143. }