logger.js 5.5 KB

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