<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>日志可视化工具</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
height: 100vh;
overflow: hidden;
}
#sidebar {
width: 25%;
border-right: 1px solid #ccc;
padding: 10px;
box-sizing: border-box;
overflow-y: auto;
}
#main {
width: 75%;
padding: 10px;
box-sizing: border-box;
overflow-y: auto;
}
textarea {
width: 100%;
height: 150px;
box-sizing: border-box;
padding: 10px;
font-family: monospace;
font-size: 14px;
}
#parseButton {
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
}
.node {
cursor: pointer;
padding: 5px;
border: 1px solid #999;
border-radius: 4px;
margin: 5px 0;
background-color: #f9f9f9;
position: relative;
}
.node:hover {
background-color: #e6f7ff;
}
.children {
margin-left: 20px;
display: none;
}
.expanded > .children {
display: block;
}
.thread-id {
font-weight: bold;
}
.log-entry {
border-left: 2px solid #ccc;
margin-left: 10px;
padding-left: 10px;
position: relative;
}
.log-entry::before {
content: '';
position: absolute;
left: -6px;
top: 10px;
width: 10px;
height: 10px;
background-color: #ccc;
border-radius: 50%;
}
.log-entry.marked {
opacity: 0.5;
}
.log-header {
cursor: pointer;
display: flex;
align-items: center;
}
.log-header .toggle-btn {
margin-right: 5px;
cursor: pointer;
font-weight: bold;
}
.log-details {
display: none;
margin-top: 5px;
margin-left: 15px;
border-left: 2px solid #ddd;
padding-left: 10px;
}
.log-header.expanded + .log-details {
display: block;
}
.log-level-I {
color: green;
font-weight: bold;
}
.log-level-E {
color: red;
font-weight: bold;
}
/* Dragging styles */
.draggable {
position: relative;
}
.draggable.dragging {
opacity: 0.5;
}
</style>
</head>
<body>
<div id="sidebar">
<h2>日志输入</h2>
<textarea id="logInput" placeholder="粘贴日志内容在这里..."></textarea>
<button id="parseButton">解析日志</button>
<h2>线程关系</h2>
<div id="threadTree"></div>
</div>
<div id="main">
<h2>日志跟踪</h2>
<div id="logTrace"></div>
</div>
<!-- Include D3.js from CDN -->
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
// 数据结构
let threads = {};
let threadTreeData = [];
let logsByThread = {};
document.getElementById('parseButton').addEventListener('click', function() {
const logText = document.getElementById('logInput').value;
parseLogs(logText);
buildThreadTree();
renderThreadTree();
document.getElementById('logTrace').innerHTML = '';
});
function parseLogs(logText) {
threads = {};
threadTreeData = [];
logsByThread = {};
const logLines = logText.split('\\n').filter(line => line.trim() !== '');
const threadStartTimes = {};
logLines.forEach(line => {
const regex = /^(I|E)\\d{3}:(\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3})\\((\\d+)\\|(\\d+)\\)([^:]+): (.+)$/;
const match = line.match(regex);
if (match) {
const level = match[1];
const timestamp = match[2];
const processId = match[3];
const threadId = match[4];
const category = match[5];
const content = match[6];
if (!logsByThread[threadId]) {
logsByThread[threadId] = [];
}
logsByThread[threadId].push({
level,
timestamp,
processId,
threadId,
category,
content,
raw: line
});
// Track thread start time
if (!threadStartTimes[threadId]) {
threadStartTimes[threadId] = timestampToMs(timestamp);
}
}
});
// Build thread relationships based on start times (simple parent assignment)
const sortedThreads = Object.keys(threadStartTimes).sort((a, b) => threadStartTimes[a] - threadStartTimes[b]);
sortedThreads.forEach(threadId => {
// Simple assumption: threads started earlier are parents
let parent = null;
for (let i = sortedThreads.indexOf(threadId) - 1; i >= 0; i--) {
const potentialParent = sortedThreads[i];
if (threadStartTimes[potentialParent] < threadStartTimes[threadId]) {
parent = potentialParent;
break;
}
}
if (parent) {
if (!threads[parent]) {
threads[parent] = { id: parent, children: [] };
}
threads[parent].children.push({ id: threadId, children: [] });
} else {
threadTreeData.push({ id: threadId, children: [] });
}
});
}
function timestampToMs(timestamp) {
const [date, time] = timestamp.split(' ');
const [month, day] = date.split('-').map(Number);
const [hour, minute, secondMs] = time.split(':');
const [second, ms] = secondMs.split('.');
return ((month * 30 + day) * 24 + Number(hour)) * 3600000 + Number(minute) * 60000 + Number(second) * 1000 + Number(ms);
}
function buildThreadTree() {
// Convert threads object to hierarchical data
// Already built in parseLogs
}
function renderThreadTree() {
const container = document.getElementById('threadTree');
container.innerHTML = '';
threadTreeData.forEach(thread => {
const node = createThreadNode(thread);
container.appendChild(node);
});
}
function createThreadNode(thread) {
const div = document.createElement('div');
div.classList.add('node');
div.innerHTML = `<span class="thread-id">线程 ${thread.id}</span>`;
div.addEventListener('click', function(e) {
e.stopPropagation();
document.querySelectorAll('#threadTree .node').forEach(n => n.classList.remove('selected'));
div.classList.add('selected');
renderLogTrace(thread.id);
});
if (thread.children && thread.children.length > 0) {
div.classList.add('expanded');
const toggleBtn = document.createElement('span');
toggleBtn.textContent = '-';
toggleBtn.classList.add('toggle-btn');
toggleBtn.addEventListener('click', function(e) {
e.stopPropagation();
div.classList.toggle('expanded');
toggleBtn.textContent = div.classList.contains('expanded') ? '-' : '+';
});
div.insertBefore(toggleBtn, div.firstChild);
const childrenDiv = document.createElement('div');
childrenDiv.classList.add('children');
thread.children.forEach(childThread => {
const childNode = createThreadNode(childThread);
childrenDiv.appendChild(childNode);
});
div.appendChild(childrenDiv);
}
return div;
}
function renderLogTrace(threadId) {
const logs = logsByThread[threadId] || [];
const traceContainer = document.getElementById('logTrace');
traceContainer.innerHTML = '';
// Build hierarchical log structure
const root = { children: [] };
const stack = [root];
logs.forEach(log => {
const current = stack[stack.length - 1];
if (log.content.endsWith('Entered...')) {
const funcName = log.content.replace(' Entered...', '');
const node = { log, children: [] };
current.children.push(node);
stack.push(node);
} else if (log.content.endsWith('Exited...')) {
stack.pop();
} else {
current.children.push({ log, children: [] });
}
});
// Render the hierarchical structure
const ul = document.createElement('ul');
buildLogList(root.children, ul);
traceContainer.appendChild(ul);
}
function buildLogList(logNodes, parentElement) {
logNodes.forEach(node => {
const li = document.createElement('li');
li.classList.add('log-entry');
// Log Header
const header = document.createElement('div');
header.classList.add('log-header');
const toggleBtn = document.createElement('span');
if (node.children.length > 0) {
toggleBtn.textContent = '+';
toggleBtn.classList.add('toggle-btn');
toggleBtn.addEventListener('click', function(e) {
e.stopPropagation();
header.classList.toggle('expanded');
toggleBtn.textContent = header.classList.contains('expanded') ? '-' : '+';
});
}
const levelSpan = document.createElement('span');
levelSpan.classList.add(`log-level-${node.log.level}`);
levelSpan.textContent = `[${node.log.level}] `;
const timeSpan = document.createElement('span');
timeSpan.textContent = `${node.log.timestamp} `;
const contentSpan = document.createElement('span');
contentSpan.textContent = node.log.content;
header.appendChild(toggleBtn);
header.appendChild(levelSpan);
header.appendChild(timeSpan);
header.appendChild(contentSpan);
// Click to mark/unmark
header.addEventListener('click', function() {
li.classList.toggle('marked');
});
li.appendChild(header);
// Log Details (optional, can be expanded)
const details = document.createElement('div');
details.classList.add('log-details');
details.textContent = node.log.raw;
li.appendChild(details);
// Recursion for children
if (node.children.length > 0) {
const childUl = document.createElement('ul');
buildLogList(node.children, childUl);
li.appendChild(childUl);
}
parentElement.appendChild(li);
});
}
// Optional: Implement drag-and-drop for thread nodes
// Note: For simplicity and brevity, drag-and-drop functionality is not fully implemented.
// It would require additional event handling and state management.
</script>
</body>
</html>