html
<!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;
flex-direction: column;
height: 100vh;
}
header {
padding: 10px;
background-color: #2c3e50;
color: white;
text-align: center;
}
main {
flex: 1;
display: flex;
overflow: hidden;
}
#leftPane, #rightPane {
padding: 10px;
box-sizing: border-box;
overflow: auto;
}
#leftPane {
width: 30%;
border-right: 1px solid #ccc;
}
#rightPane {
width: 70%;
}
textarea {
width: 100%;
height: 150px;
resize: vertical;
padding: 10px;
box-sizing: border-box;
font-family: monospace;
font-size: 14px;
}
#processButton {
margin-top: 10px;
padding: 10px 20px;
background-color: #27ae60;
color: white;
border: none;
cursor: pointer;
font-size: 16px;
}
#processButton:hover {
background-color: #219150;
}
/* Thread Relationship Styles */
.node {
cursor: pointer;
stroke: #3182bd;
stroke-width: 1.5px;
}
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
/* Log Trace Styles */
.log-entry {
border-bottom: 1px solid #ddd;
padding: 5px 0;
}
.log-header {
display: flex;
justify-content: space-between;
cursor: pointer;
background-color: #f9f9f9;
padding: 5px;
margin: 2px 0;
}
.log-content {
padding-left: 20px;
display: none;
}
.important {
opacity: 1 !important;
}
.not-important {
opacity: 0.5;
}
/* Checkbox Styles */
.log-header input {
margin-right: 10px;
}
</style>
</head>
<body>
<header>
<h1>日志节点化可视化展示工具</h1>
</header>
<main>
<div style="width: 100%; padding: 10px; box-sizing: border-box;">
<textarea id="logInput" placeholder="在此粘贴日志内容..."></textarea>
<button id="processButton">解析并展示日志</button>
</div>
</main>
<main style="flex: 1; display: flex;">
<div id="leftPane">
<h2>线程关系</h2>
<svg width="100%" height="600"></svg>
</div>
<div id="rightPane">
<h2>日志跟踪</h2>
<div id="logTrace"></div>
</div>
</main>
<!-- D3.js Library -->
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
// 数据结构
let threads = {};
let threadTree = { name: "主线程", children: [] };
let currentSelectedThread = null;
document.getElementById('processButton').addEventListener('click', function() {
const logText = document.getElementById('logInput').value;
parseLogs(logText);
buildThreadRelation();
renderThreadTree();
});
// 解析日志
function parseLogs(logText) {
const lines = logText.split('\n').filter(line => line.trim() !== '');
threads = {}; // Reset
lines.forEach(line => {
const logRegex = /^([IE]\d{3}):(\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\((\d+)\|(\d+)\)([^:]+):\s*(.*)$/;
const match = line.match(logRegex);
if (match) {
const level = match[1].startsWith('I') ? '信息' : '错误';
const timestamp = match[2];
const processId = match[3];
const threadId = match[4];
const module = match[5];
const content = match[6];
if (!threads[threadId]) {
threads[threadId] = {
threadId,
processId,
logs: [],
children: []
};
}
threads[threadId].logs.push({
level,
timestamp,
module,
content,
raw: line
});
}
});
// Build thread relationships
// For simplicity, assuming threads are created sequentially
const threadIds = Object.keys(threads);
threadTree = { name: "主线程", children: [] };
threadIds.forEach((tid, index) => {
if (index === 0) {
threadTree.children.push(threads[tid]);
} else {
// Simple parent assignment
threadTree.children[threadIds[index -1].threadId] = threads[tid];
}
});
}
// 构建线程关系树
function buildThreadRelation() {
// Placeholder for actual thread relationship building
// Here we assume all threads are children of the main thread
threadTree = { name: "主线程", children: [] };
Object.values(threads).forEach(thread => {
threadTree.children.push({
name: `线程 ${thread.threadId}`,
children: []
});
});
}
// 渲染线程关系树
function renderThreadTree() {
const svg = d3.select("#leftPane svg");
svg.selectAll("*").remove();
const width = document.getElementById('leftPane').clientWidth;
const height = 600;
const root = d3.hierarchy(threadTree);
const treeLayout = d3.tree().size([height, width - 160]);
treeLayout(root);
// Links
svg.selectAll('.link')
.data(root.links())
.enter()
.append('path')
.attr('class', 'link')
.attr('d', d3.linkHorizontal()
.x(d => d.y)
.y(d => d.x)
);
// Nodes
const node = svg.selectAll('.node')
.data(root.descendants())
.enter()
.append('g')
.attr('class', 'node')
.attr('transform', d => `translate(${d.y},${d.x})`)
.on('click', (event, d) => {
if(d.data.threadId){
currentSelectedThread = d.data.threadId;
renderLogTrace(d.data.threadId);
}
});
node.append('circle')
.attr('r', 5)
.attr('fill', d => d.data.threadId ? "#ff5722" : "#4285f4");
node.append('text')
.attr('dy', 3)
.attr('x', d => d.children ? -10 : 10)
.style('text-anchor', d => d.children ? 'end' : 'start')
.text(d => d.data.name);
}
// 渲染日志跟踪
function renderLogTrace(threadId) {
const logContainer = document.getElementById('logTrace');
logContainer.innerHTML = '';
const logs = threads[threadId].logs;
const logHierarchy = buildLogHierarchy(logs);
logHierarchy.forEach(entry => {
const logElement = createLogElement(entry);
logContainer.appendChild(logElement);
});
}
// 构建日志层级
function buildLogHierarchy(logs) {
const stack = [];
const hierarchy = [];
logs.forEach(log => {
if (log.content.endsWith('Entered...')) {
const funcName = log.content.replace(' Entered...', '');
const entry = { ...log, children: [] };
if (stack.length === 0) {
hierarchy.push(entry);
} else {
stack[stack.length -1].children.push(entry);
}
stack.push(entry);
} else if (log.content.endsWith('Exited...')) {
stack.pop();
} else {
const entry = { ...log, children: [] };
if (stack.length === 0) {
hierarchy.push(entry);
} else {
stack[stack.length -1].children.push(entry);
}
}
});
return hierarchy;
}
// 创建日志元素
function createLogElement(entry) {
const div = document.createElement('div');
div.className = 'log-entry';
const header = document.createElement('div');
header.className = 'log-header';
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.checked = true;
checkbox.addEventListener('change', () => {
if (checkbox.checked) {
content.style.opacity = 1;
} else {
content.style.opacity = 0.5;
}
});
header.appendChild(checkbox);
const headerText = document.createElement('span');
headerText.textContent = `[${entry.timestamp}] [${entry.level}] ${entry.content}`;
header.appendChild(headerText);
header.addEventListener('click', () => {
if (content.style.display === 'none') {
content.style.display = 'block';
} else {
content.style.display = 'none';
}
});
div.appendChild(header);
const content = document.createElement('div');
content.className = 'log-content';
content.textContent = entry.raw;
div.appendChild(content);
if (entry.children && entry.children.length > 0) {
entry.children.forEach(child => {
const childElement = createLogElement(child);
content.appendChild(childElement);
});
}
return div;
}
</script>
</body>
</html>