logger.js 4.4 KB

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