html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>日志可视化工具</title>
<!-- Vis.js CDN -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.min.css" rel="stylesheet" type="text/css" />
<!-- 简单的重置样式 -->
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
font-family: Arial, sans-serif;
}
header {
padding: 10px;
background-color: #2c3e50;
color: white;
text-align: center;
}
#controls {
padding: 10px;
background-color: #ecf0f1;
}
#controls textarea {
width: 100%;
height: 150px;
resize: vertical;
padding: 10px;
box-sizing: border-box;
font-family: monospace;
font-size: 14px;
}
#controls button {
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
background-color: #2980b9;
color: white;
border: none;
cursor: pointer;
border-radius: 4px;
}
#controls button:hover {
background-color: #3498db;
}
#main {
display: flex;
height: calc(100% - 220px);
}
#thread-container, #log-container {
flex: 1;
border: 1px solid #bdc3c7;
box-sizing: border-box;
position: relative;
}
#thread-container {
border-right: none;
}
#thread-container .network-title,
#log-container .network-title {
position: absolute;
top: 5px;
left: 10px;
background: rgba(255,255,255,0.8);
padding: 2px 5px;
border-radius: 3px;
font-weight: bold;
z-index: 1;
}
#thread-network, #log-network {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<header>
<h1>日志可视化工具</h1>
</header>
<div id="controls">
<textarea id="log-input" placeholder="在此处粘贴日志内容..."></textarea>
<button id="parse-button">解析并可视化</button>
</div>
<div id="main">
<div id="thread-container">
<div class="network-title">线程关系</div>
<div id="thread-network"></div>
</div>
<div id="log-container">
<div class="network-title">日志跟踪</div>
<div id="log-network"></div>
</div>
</div>
<script>
// 全局变量存储解析后的日志数据
const parsedLogs = {};
const threadRelations = {};
const threadParents = {};
document.getElementById('parse-button').addEventListener('click', function() {
const logText = document.getElementById('log-input').value;
parseLogs(logText);
renderThreadNetwork();
// 清右侧日志跟踪
renderEmptyLogNetwork();
});
/**
* 解析日志文本
* @param {string} text - 日志文本
*/
function parseLogs(text) {
const lines = text.split('\n').filter(line => line.trim() !== '');
parsedLogs.clear = {}; // Clear previous data
for (const line of lines) {
const regex = /^(I|E)\d{3}:(\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\((\d+)\|(\d+)\)Sum\.Rpt: (.+)$/;
const match = line.match(regex);
if (match) {
const [_, level, timestamp, processId, threadId, content] = match;
if (!parsedLogs[threadId]) {
parsedLogs[threadId] = [];
}
parsedLogs[threadId].push({
level,
timestamp,
processId,
threadId,
content,
raw: line
});
}
}
// Process thread relationships if possible
inferThreadRelationships();
}
/**
* 推断线程之间的关系
* 这里假设如果一个线程在另一个线程的日志时间之后首次出现,则视为被启动
* 这是一个简化的假设,实际情况可能需要更多信息
*/
function inferThreadRelationships() {
threadRelations.clear = {};
threadParents.clear = {};
// 获取每个线程的最早时间
const threadStartTimes = {};
for (const threadId in parsedLogs) {
const firstLog = parsedLogs[threadId][0];
threadStartTimes[threadId] = new Date(`2023/10/26 ${firstLog.timestamp.replace(/-/g, '/').replace('.', ':')}`);
}
// 排序线程按开始时间
const sortedThreads = Object.keys(threadStartTimes).sort((a, b) => threadStartTimes[a] - threadStartTimes[b]);
// 简单地将后启动的线程归为最早启动线程的子线程
for (let i = 1; i < sortedThreads.length; i++) {
const childThread = sortedThreads[i];
const parentThread = sortedThreads[0];
threadParents[childThread] = parentThread;
}
}
/**
* 渲染线程关系网络
*/
function renderThreadNetwork() {
const nodes = [];
const edges = [];
const uniqueThreads = Object.keys(parsedLogs);
// 创建节点
uniqueThreads.forEach(threadId => {
nodes.push({id: threadId, label: `线程 ${threadId}`, shape: 'box', color: '#blue'});
});
// 创建边
for (const child in threadParents) {
const parent = threadParents[child];
edges.push({from: parent, to: child});
}
const data = { nodes: new vis.DataSet(nodes), edges: new vis.DataSet(edges) };
const options = {
layout: {
hierarchical: {
enabled: true,
direction: 'UD',
sortMethod: 'directed'
}
},
interaction: { multiselect: false },
physics: { hierarchicalRepulsion: { nodeDistance: 150 } },
nodes: {
shape: 'box',
color: {
background: '#81C784',
border: '#388E3C',
highlight: {
background: '#4CAF50',
border: '#2E7D32'
}
},
font: { color: '#ffffff', size: 14 },
},
edges: { arrows: 'to', smooth: true }
};
const network = new vis.Network(document.getElementById('thread-network'), data, options);
// 点击事件
network.on("click", function (params) {
if (params.nodes.length > 0) {
const threadId = params.nodes[0];
renderLogNetwork(threadId);
}
});
// 右键事件
network.on("oncontext", function (params) {
params.event.preventDefault();
const pointer = params.pointer.DOM;
network.getNodeAt(pointer, function(nodeId) {
if (nodeId) {
toggleNodeImportance(nodeId, network);
}
});
});
}
/**
* 渲染空的日志跟踪网络
*/
function renderEmptyLogNetwork() {
const container = document.getElementById('log-network');
container.innerHTML = '<p style="text-align:center; padding-top:20px; color:#7f8c8d;">请选择左侧的线程以查看日志</p>';
}
/**
* 渲染指定线程的日志跟踪网络
* @param {string} threadId - 线程ID
*/
function renderLogNetwork(threadId) {
const logs = parsedLogs[threadId];
if (!logs) {
alert('没有找到该线程的日志。');
return;
}
// 构建层级结构
const tree = buildLogTree(logs);
// 转换为vis.js的数据格式
const nodes = [];
const edges = [];
let nodeId = 1;
function traverse(node, parentId = null) {
const currentId = nodeId++;
let label = `[${node.level}] ${node.timestamp} ${node.content}`;
nodes.push({
id: currentId,
label: label,
font: { size: 14 },
color: {
background: node.level === 'E' ? '#e57373' : '#64b5f6',
border: '#424242',
highlight: {
background: node.level === 'E' ? '#ef5350' : '#42a5f5',
border: '#212121'
}
}
});
if (parentId !== null) {
edges.push({ from: parentId, to: currentId });
}
if (node.children) {
node.children.forEach(child => traverse(child, currentId));
}
}
tree.forEach(rootNode => traverse(rootNode));
const data = { nodes: new vis.DataSet(nodes), edges: new vis.DataSet(edges) };
const options = {
layout: {
hierarchical: {
enabled: true,
direction: 'UD',
sortMethod: 'directed'
}
},
interaction: { multiselect: false },
physics: { hierarchicalRepulsion: { nodeDistance: 150 } },
nodes: {
shape: 'box',
font: { color: '#ffffff', size: 14 },
},
edges: { arrows: 'to', smooth: true }
};
// 清空之前的网络
document.getElementById('log-network').innerHTML = '';
const network = new vis.Network(document.getElementById('log-network'), data, options);
// 右键事件
network.on("oncontext", function (params) {
params.event.preventDefault();
const pointer = params.pointer.DOM;
network.getNodeAt(pointer, function(nodeId) {
if (nodeId) {
toggleNodeImportance(nodeId, network);
}
});
});
}
/**
* 构建日志树结构
* @param {Array} logs - 日志数组
* @returns {Array} - 树结构数组
*/
function buildLogTree(logs) {
const stack = [];
const tree = [];
logs.forEach(log => {
const isEntry = /Entered\.\.\.$/.test(log.content);
const isExit = /Exited\.\.\.$/.test(log.content);
if (isEntry) {
const node = { ...log, children: [] };
if (stack.length === 0) {
tree.push(node);
} else {
stack[stack.length - 1].children.push(node);
}
stack.push(node);
} else if (isExit) {
stack.pop();
} else {
const node = { ...log };
if (stack.length === 0) {
tree.push(node);
} else {
stack[stack.length - 1].children.push(node);
}
}
});
return tree;
}
/**
* 切换节点的重要性(透明度)
* @param {number|string} nodeId - 节点ID
* @param {vis.Network} network - 网络实例
*/
function toggleNodeImportance(nodeId, network) {
const node = network.body.data.nodes.get(nodeId);
if (node.opacity === 0.5) {
network.body.data.nodes.update({ id: nodeId, opacity: 1.0 });
} else {
network.body.data.nodes.update({ id: nodeId, opacity: 0.5 });
}
}
/**
* 清除 DataSet
*/
DataSet.prototype.clear = function() {
this.remove(this.getIds());
};
</script>
</body>
</html>