logger.js 5.8 KB

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