logger.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.colors = {
  32. Reset: "\x1b[0m",
  33. Bright: "\x1b[1m",
  34. Dim: "\x1b[2m",
  35. Underscore: "\x1b[4m",
  36. Blink: "\x1b[5m",
  37. Reverse: "\x1b[7m",
  38. Hidden: "\x1b[8m",
  39. FgBlack: "\x1b[30m",
  40. FgRed: "\x1b[31m",
  41. FgGreen: "\x1b[32m",
  42. FgYellow: "\x1b[33m",
  43. FgBlue: "\x1b[34m",
  44. FgMagenta: "\x1b[35m",
  45. FgCyan: "\x1b[36m",
  46. FgWhite: "\x1b[37m",
  47. BgBlack: "\x1b[40m",
  48. BgRed: "\x1b[41m",
  49. BgGreen: "\x1b[42m",
  50. BgYellow: "\x1b[43m",
  51. BgBlue: "\x1b[44m",
  52. BgMagenta: "\x1b[45m",
  53. BgCyan: "\x1b[46m",
  54. BgWhite: "\x1b[47m"
  55. };
  56. fs.appendFile(this.configDirectory + '/all.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  57. fs.appendFile(this.configDirectory + '/success.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  58. fs.appendFile(this.configDirectory + '/error.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  59. fs.appendFile(this.configDirectory + '/info.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  60. fs.appendFile(this.configDirectory + '/debugStation.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  61. for(let i = 0; i < this.reservedLines; i++) {
  62. process.stdout.write("\n");
  63. }
  64. resolve();
  65. });
  66. }
  67. async success(type, text, display = true) {
  68. try { await this._validateHook(); } catch { return; }
  69. const time = getTimeFormatted();
  70. const message = `${time} SUCCESS - ${type} - ${text}`;
  71. this.writeFile('all.log', message);
  72. this.writeFile('success.log', message);
  73. if (display) this.log(this.colors.FgGreen, message);
  74. }
  75. async error(type, text, display = true) {
  76. try { await this._validateHook(); } catch { return; }
  77. const time = getTimeFormatted();
  78. const message = `${time} ERROR - ${type} - ${text}`;
  79. this.writeFile('all.log', message);
  80. this.writeFile('error.log', message);
  81. if (display) this.log(this.colors.FgRed, message);
  82. }
  83. async info(type, text, display = true) {
  84. try { await this._validateHook(); } catch { return; }
  85. const time = getTimeFormatted();
  86. const message = `${time} INFO - ${type} - ${text}`;
  87. this.writeFile('all.log', message);
  88. this.writeFile('info.log', message);
  89. if (display) this.log(this.colors.FgCyan, message);
  90. }
  91. async stationIssue(text, display = false) {
  92. try { await this._validateHook(); } catch { return; }
  93. const time = getTimeFormatted();
  94. const message = `${time} DEBUG_STATION - ${text}`;
  95. this.writeFile('debugStation.log', message);
  96. if (display) this.log(this.colors.FgMagenta, message);
  97. }
  98. log(color, message) {
  99. process.stdout.moveCursor(0, -this.reservedLines);
  100. process.stdout.write(`${color}${message}${this.colors.Reset}\n`);
  101. for(let i = 0; i < this.reservedLines; i++) {
  102. process.stdout.write("\n");
  103. }
  104. this.moduleManager.printStatus();
  105. }
  106. writeFile(fileName, message) {
  107. fs.appendFile(`${this.configDirectory}/${fileName}`, `${message}\n`, ()=>{});
  108. }
  109. }