log-stream.js 3.7 KB

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