logger.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. process.stdout.write(Array(this.reservedLines).fill(`\n`).join(""));
  68. }
  69. resolve();
  70. });
  71. }
  72. async success(type, text, display = true) {
  73. try { await this._validateHook(); } catch { return; }
  74. const time = getTimeFormatted();
  75. const message = `${time} SUCCESS - ${type} - ${text}`;
  76. this.writeFile('all.log', message);
  77. this.writeFile('success.log', message);
  78. if (display) this.log(this.colors.FgGreen, message);
  79. }
  80. async error(type, text, display = true) {
  81. try { await this._validateHook(); } catch { return; }
  82. const time = getTimeFormatted();
  83. const message = `${time} ERROR - ${type} - ${text}`;
  84. this.writeFile('all.log', message);
  85. this.writeFile('error.log', message);
  86. if (display) this.log(this.colors.FgRed, message);
  87. }
  88. async info(type, text, display = true) {
  89. try { await this._validateHook(); } catch { return; }
  90. const time = getTimeFormatted();
  91. const message = `${time} INFO - ${type} - ${text}`;
  92. this.writeFile('all.log', message);
  93. this.writeFile('info.log', message);
  94. if (display) this.log(this.colors.FgCyan, message);
  95. }
  96. async debug(text, display = true) {
  97. try { await this._validateHook(); } catch { return; }
  98. const time = getTimeFormatted();
  99. const message = `${time} DEBUG - ${text}`;
  100. if (display) this.log(this.colors.FgMagenta, message);
  101. }
  102. async stationIssue(text, display = false) {
  103. try { await this._validateHook(); } catch { return; }
  104. const time = getTimeFormatted();
  105. const message = `${time} DEBUG_STATION - ${text}`;
  106. this.writeFile('debugStation.log', message);
  107. if (display) this.log(this.colors.FgMagenta, message);
  108. }
  109. log(color, message) {
  110. if (this.moduleManager.fancyConsole) {
  111. const rows = process.stdout.rows;
  112. const columns = process.stdout.columns;
  113. const lineNumber = rows - this.reservedLines;
  114. let lines = 0;
  115. message.split("\n").forEach((line) => {
  116. lines += Math.floor(line.replace("\t", " ").length / columns) + 1;
  117. });
  118. if (lines > this.logger.reservedLines)
  119. lines = this.logger.reservedLines;
  120. process.stdout.cursorTo(0, rows - this.logger.reservedLines);
  121. process.stdout.clearScreenDown();
  122. process.stdout.cursorTo(0, lineNumber);
  123. process.stdout.write(`${color}${message}${this.colors.Reset}\n`);
  124. process.stdout.cursorTo(0, process.stdout.rows);
  125. process.stdout.write(Array(lines).fill(`\n!`).join(""));
  126. this.moduleManager.printStatus();
  127. } else console.log(`${color}${message}${this.colors.Reset}`);
  128. }
  129. writeFile(fileName, message) {
  130. fs.appendFile(`${this.configDirectory}/${fileName}`, `${message}\n`, ()=>{});
  131. }
  132. }