logs.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. (function (window, $) {
  2. var gLogs = [];
  3. App.prototype.init = function connectToLogs () {
  4. var app = this,
  5. $logs = $('#logs'),
  6. logTabs = {
  7. 'ALL': $('#all'),
  8. 'KEYS': $('#keys'),
  9. 'DEBUG': $('#debug'),
  10. 'INFO': $('#info'),
  11. 'WARN': $('#warn'),
  12. 'ERROR': $('#error'),
  13. 'CRITICAL': $('#critical')
  14. },
  15. logCount = {
  16. 'ALL': 0,
  17. 'KEYS': 0,
  18. 'DEBUG': 0,
  19. 'INFO': 0,
  20. 'WARN': 0,
  21. 'ERROR': 0,
  22. 'CRITICAL': 0
  23. },
  24. pause = $('#pause'),
  25. ts = (new Date()).getTime() - 60000,
  26. tmpl = this.render([
  27. '<div class="new-message message">',
  28. '<strong>[%time%]</strong> %level% ',
  29. '<i>%file%%line%</i> ',
  30. '<span class="message-text">%msg%</span>',
  31. '</div>'].join(''));
  32. function makeHtml (msg) {
  33. var time = msg && msg.time ? new Date(msg.time * 1000) : new Date(),
  34. text = [];
  35. function fillZero (num) {
  36. if (num < 10) {
  37. return '0' + num;
  38. }
  39. return num;
  40. }
  41. text.push(fillZero(time.getHours()));
  42. text.push(fillZero(time.getMinutes()));
  43. text.push(fillZero(time.getSeconds()));
  44. return tmpl({
  45. time: text.join(':'),
  46. level: msg.level,
  47. file: msg.file,
  48. line: msg.line ? ':' + msg.line : '',
  49. msg: msg.message
  50. });
  51. }
  52. function getNewLogs() {
  53. if (pause.is('.active')) {
  54. window.setTimeout(getNewLogs, 1000);
  55. return;
  56. }
  57. app.send('/logs', {ts:ts}, function (resp) {
  58. var logs;
  59. if (resp.logs && resp.logs.length > 0) {
  60. gLogs = gLogs.concat(resp.logs);
  61. resp.logs.forEach(function (log) {
  62. var logHtml = makeHtml(log);
  63. logTabs[log.level].prepend(logHtml);
  64. logTabs.ALL.prepend(logHtml);
  65. logCount[log.level] += 1;
  66. logCount.ALL += 1;
  67. });
  68. $('#countALL').html('(' + logCount.ALL + ')');
  69. $('#countKEYS').html('(' + logCount.KEYS + ')');
  70. $('#countDEBUG').html('(' + logCount.DEBUG + ')');
  71. $('#countINFO').html('(' + logCount.INFO + ')');
  72. $('#countWARN').html('(' + logCount.WARN + ')');
  73. $('#countERROR').html('(' + logCount.ERROR + ')');
  74. $('#countCRITICAL').html('(' + logCount.CRITICAL + ')');
  75. }
  76. if (resp.ts) {
  77. ts = resp.ts;
  78. }
  79. window.setTimeout(getNewLogs, 1000);
  80. });
  81. }
  82. getNewLogs();
  83. };
  84. new App();
  85. } (window, jQuery));