logger.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. 'use strict';
  2. const dir = `${__dirname}/../../log`;
  3. const fs = require('fs');
  4. const config = require('config');
  5. //const Discord = require("discord.js");
  6. let client;
  7. let utils;
  8. if (!config.isDocker && !fs.existsSync(`${dir}`)) {
  9. fs.mkdirSync(dir);
  10. }
  11. let started;
  12. let success = 0;
  13. let successThisMinute = 0;
  14. let successThisHour = 0;
  15. let error = 0;
  16. let errorThisMinute = 0;
  17. let errorThisHour = 0;
  18. let info = 0;
  19. let infoThisMinute = 0;
  20. let infoThisHour = 0;
  21. let successUnitsPerMinute = [0,0,0,0,0,0,0,0,0,0];
  22. let errorUnitsPerMinute = [0,0,0,0,0,0,0,0,0,0];
  23. let infoUnitsPerMinute = [0,0,0,0,0,0,0,0,0,0];
  24. let successUnitsPerHour = [0,0,0,0,0,0,0,0,0,0];
  25. let errorUnitsPerHour = [0,0,0,0,0,0,0,0,0,0];
  26. let infoUnitsPerHour = [0,0,0,0,0,0,0,0,0,0];
  27. function calculateUnits(units, unit) {
  28. units.push(unit);
  29. if (units.length > 10) units.shift();
  30. return units;
  31. }
  32. function calculateHourUnits() {
  33. successUnitsPerHour = calculateUnits(successUnitsPerHour, successThisHour);
  34. errorUnitsPerHour = calculateUnits(errorUnitsPerHour, errorThisHour);
  35. infoUnitsPerHour = calculateUnits(infoUnitsPerHour, infoThisHour);
  36. successThisHour = 0;
  37. errorThisHour = 0;
  38. infoThisHour = 0;
  39. utils.emitToRoom('admin.statistics', 'event:admin.statistics.success.units.hour', successUnitsPerHour);
  40. utils.emitToRoom('admin.statistics', 'event:admin.statistics.error.units.hour', errorUnitsPerHour);
  41. utils.emitToRoom('admin.statistics', 'event:admin.statistics.info.units.hour', infoUnitsPerHour);
  42. setTimeout(calculateHourUnits, 1000 * 60 * 60)
  43. }
  44. function calculateMinuteUnits() {
  45. successUnitsPerMinute = calculateUnits(successUnitsPerMinute, successThisMinute);
  46. errorUnitsPerMinute = calculateUnits(errorUnitsPerMinute, errorThisMinute);
  47. infoUnitsPerMinute = calculateUnits(infoUnitsPerMinute, infoThisMinute);
  48. successThisMinute = 0;
  49. errorThisMinute = 0;
  50. infoThisMinute = 0;
  51. utils.emitToRoom('admin.statistics', 'event:admin.statistics.success.units.minute', successUnitsPerMinute);
  52. utils.emitToRoom('admin.statistics', 'event:admin.statistics.error.units.minute', errorUnitsPerMinute);
  53. utils.emitToRoom('admin.statistics', 'event:admin.statistics.info.units.minute', infoUnitsPerMinute);
  54. setTimeout(calculateMinuteUnits, 1000 * 60)
  55. }
  56. let twoDigits = (num) => {
  57. return (num < 10) ? '0' + num : num;
  58. };
  59. let getTime = () => {
  60. let time = new Date();
  61. return {
  62. year: time.getFullYear(),
  63. month: time.getMonth() + 1,
  64. day: time.getDate(),
  65. hour: time.getHours(),
  66. minute: time.getMinutes(),
  67. second: time.getSeconds()
  68. }
  69. };
  70. let getTimeFormatted = () => {
  71. let time = getTime();
  72. return `${time.year}-${twoDigits(time.month)}-${twoDigits(time.day)} ${twoDigits(time.hour)}:${twoDigits(time.minute)}:${twoDigits(time.second)}`;
  73. }
  74. let initialized = false;
  75. let lockdown = false;
  76. module.exports = {
  77. init: function(cb) {
  78. utils = require('./utils');
  79. started = Date.now();
  80. setTimeout(calculateMinuteUnits, 1000 * 60);
  81. setTimeout(calculateHourUnits, 1000 * 60 * 60);
  82. setTimeout(this.calculate, 1000 * 30);
  83. let time = getTimeFormatted();
  84. fs.appendFile(dir + '/all.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  85. fs.appendFile(dir + '/success.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  86. fs.appendFile(dir + '/error.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  87. fs.appendFile(dir + '/info.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  88. fs.appendFile(dir + '/debugStation.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
  89. initialized = true;
  90. if (lockdown) return this._lockdown();
  91. cb();
  92. },
  93. success: (type, message, display = true) => {
  94. if (lockdown) return;
  95. success++;
  96. successThisMinute++;
  97. successThisHour++;
  98. let time = getTimeFormatted();
  99. fs.appendFile(dir + '/all.log', `${time} SUCCESS - ${type} - ${message}\n`, ()=>{});
  100. fs.appendFile(dir + '/success.log', `${time} SUCCESS - ${type} - ${message}\n`, ()=>{});
  101. if (display) console.info('\x1b[32m', time, 'SUCCESS', '-', type, '-', message, '\x1b[0m');
  102. },
  103. error: (type, message, display = true) => {
  104. if (lockdown) return;
  105. error++;
  106. errorThisMinute++;
  107. errorThisHour++;
  108. let time = getTimeFormatted();
  109. fs.appendFile(dir + '/all.log', `${time} ERROR - ${type} - ${message}\n`, ()=>{});
  110. fs.appendFile(dir + '/error.log', `${time} ERROR - ${type} - ${message}\n`, ()=>{});
  111. if (display) console.warn('\x1b[31m', time, 'ERROR', '-', type, '-', message, '\x1b[0m');
  112. },
  113. info: (type, message, display = true) => {
  114. if (lockdown) return;
  115. info++;
  116. infoThisMinute++;
  117. infoThisHour++;
  118. let time = getTimeFormatted();
  119. fs.appendFile(dir + '/all.log', `${time} INFO - ${type} - ${message}\n`, ()=>{});
  120. fs.appendFile(dir + '/info.log', `${time} INFO - ${type} - ${message}\n`, ()=>{});
  121. if (display) console.info('\x1b[36m', time, 'INFO', '-', type, '-', message, '\x1b[0m');
  122. },
  123. stationIssue: (string, display = false) => {
  124. if (lockdown) return;
  125. let time = getTimeFormatted();
  126. fs.appendFile(dir + '/debugStation.log', `${time} - ${string}\n`, ()=>{});
  127. if (display) console.info('\x1b[35m', time, '-', string, '\x1b[0m');
  128. },
  129. calculatePerSecond: function(number) {
  130. if (lockdown) return;
  131. let secondsRunning = Math.floor((Date.now() - started) / 1000);
  132. let perSecond = number / secondsRunning;
  133. return perSecond;
  134. },
  135. calculatePerMinute: function(number) {
  136. if (lockdown) return;
  137. let perMinute = this.calculatePerSecond(number) * 60;
  138. return perMinute;
  139. },
  140. calculatePerHour: function(number) {
  141. if (lockdown) return;
  142. let perHour = this.calculatePerMinute(number) * 60;
  143. return perHour;
  144. },
  145. calculatePerDay: function(number) {
  146. if (lockdown) return;
  147. let perDay = this.calculatePerHour(number) * 24;
  148. return perDay;
  149. },
  150. calculate: function() {
  151. if (lockdown) return;
  152. let _this = module.exports;
  153. utils.emitToRoom('admin.statistics', 'event:admin.statistics.logs', {
  154. second: {
  155. success: _this.calculatePerSecond(success),
  156. error: _this.calculatePerSecond(error),
  157. info: _this.calculatePerSecond(info)
  158. },
  159. minute: {
  160. success: _this.calculatePerMinute(success),
  161. error: _this.calculatePerMinute(error),
  162. info: _this.calculatePerMinute(info)
  163. },
  164. hour: {
  165. success: _this.calculatePerHour(success),
  166. error: _this.calculatePerHour(error),
  167. info: _this.calculatePerHour(info)
  168. },
  169. day: {
  170. success: _this.calculatePerDay(success),
  171. error: _this.calculatePerDay(error),
  172. info: _this.calculatePerDay(info)
  173. }
  174. });
  175. setTimeout(_this.calculate, 1000 * 30);
  176. },
  177. _lockdown: () => {
  178. lockdown = true;
  179. }
  180. };