log-stream.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. let logSource = null;
  2. let currentUnit = null;
  3. const MAX_LOG_LINES = 800;
  4. const MIN_LOG_LINES = 500;
  5. let logLines = [];
  6. function escapeHtml(s) {
  7. return s
  8. .replace(/&/g, "&")
  9. .replace(/</g, "&lt;")
  10. .replace(/>/g, "&gt;");
  11. }
  12. function colorLog(line) {
  13. const s = escapeHtml(line);
  14. const levels = [
  15. ["TRACE", "log-trace"],
  16. ["DEBUG", "log-debug"],
  17. ["INFO", "log-info"],
  18. ["WARN", "log-warn"],
  19. ["WARNING", "log-warn"],
  20. ["ERROR", "log-error"],
  21. ["FATAL", "log-fatal"],
  22. ["PANIC", "log-panic"]
  23. ];
  24. for (const [lvl, cls] of levels) {
  25. if (s.includes(lvl) || s.includes(lvl.toLowerCase())) {
  26. return `<span class="${cls}">${s}</span>`;
  27. }
  28. }
  29. return `<span class="log-default">${s}</span>`;
  30. }
  31. function connectLog(unit, target) {
  32. const el = typeof target === "string"
  33. ? document.getElementById(target)
  34. : target;
  35. if (!el) {
  36. console.error("[log-stream] target element not found");
  37. return;
  38. }
  39. if (currentUnit === unit && logSource &&
  40. logSource.readyState !== EventSource.CLOSED) {
  41. return;
  42. }
  43. disconnectLog();
  44. currentUnit = unit;
  45. logLines = [];
  46. el.textContent = "";
  47. const source = new EventSource(
  48. `/api/service/log?unit=${encodeURIComponent(unit)}`
  49. );
  50. logSource = source;
  51. source.onmessage = (e) => {
  52. if (source !== logSource) {
  53. return;
  54. }
  55. logLines.push(e.data);
  56. if (logLines.length > MAX_LOG_LINES) {
  57. logLines.shift();
  58. }
  59. const line = document.createElement("div");
  60. line.innerHTML = colorLog(e.data);
  61. const atBottom = el.scrollHeight - el.scrollTop - el.clientHeight < 20;
  62. el.appendChild(line);
  63. if (el.childElementCount > MAX_LOG_LINES) {
  64. while (el.childElementCount > MIN_LOG_LINES) {
  65. el.firstElementChild.remove();
  66. }
  67. }
  68. if (atBottom) {
  69. requestAnimationFrame(() => {
  70. el.scrollTop = el.scrollHeight;
  71. });
  72. }
  73. };
  74. source.onerror = () => {
  75. if (source !== logSource) {
  76. return;
  77. }
  78. console.warn(
  79. `[log-stream] error unit=${unit}, state=${source.readyState}`
  80. );
  81. };
  82. }
  83. function disconnectLog() {
  84. if (logSource) {
  85. logSource.close();
  86. logSource = null;
  87. }
  88. currentUnit = null;
  89. logLines = [];
  90. }
  91. function getCurrentUnit() {
  92. return currentUnit;
  93. }
  94. function clearLog(target) {
  95. logLines = [];
  96. const el = typeof target === "string"
  97. ? document.getElementById(target)
  98. : target;
  99. if (el) {
  100. el.replaceChildren();
  101. el.scrollTop = 0;
  102. }
  103. }
  104. function copyLog(target) {
  105. const text = logLines.join("\n");
  106. if (navigator.clipboard) {
  107. navigator.clipboard.writeText(text)
  108. .then(() => alert("复制成功"))
  109. .catch(() => alert("复制失败"));
  110. } else {
  111. const ta = document.createElement("textarea");
  112. ta.value = text;
  113. document.body.appendChild(ta);
  114. ta.select();
  115. document.execCommand("copy");
  116. ta.remove();
  117. alert("复制成功");
  118. }
  119. }