PyBot/log.php
2026-05-20 00:40:03 +08:00

238 lines
6.3 KiB
PHP

<?php
// log.php - 日志查看页面
// 设置字符编码
header('Content-Type: text/html; charset=utf-8');
// 日志文件路径
$logFile = './resources/log/core.log';
// 检查日志文件是否存在
if (!file_exists($logFile)) {
$logContent = "日志文件不存在: " . htmlspecialchars($logFile);
} else {
// 读取日志文件内容
$logContent = file_get_contents($logFile);
// 如果文件为空
if (empty($logContent)) {
$logContent = "日志文件为空";
}
// 对内容进行HTML转义以防止XSS攻击
$logContent = htmlspecialchars($logContent, ENT_QUOTES, 'UTF-8');
}
// 获取最后修改时间
$lastModified = file_exists($logFile) ? date("Y-m-d H:i:s", filemtime($logFile)) : "未知";
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>程序运行日志</title>
<!-- 引入 Layui 的 CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui-src/dist/css/layui.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "Microsoft YaHei", Arial, sans-serif;
background-color: #f5f5f5;
padding: 20px;
line-height: 1.6;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
text-align: center;
}
.header h1 {
font-size: clamp(1.5rem, 4vw, 2.5rem);
margin-bottom: 10px;
}
.header p {
font-size: clamp(0.9rem, 2vw, 1.1rem);
opacity: 0.9;
}
.info-bar {
background: #f8f9fa;
padding: 15px 20px;
border-bottom: 1px solid #eee;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 10px;
}
.info-item {
font-size: 0.9rem;
color: #666;
}
.log-container {
padding: 20px;
max-height: 70vh;
overflow-y: auto;
}
.log-content {
background: #2d2d2d;
color: #f8f8f2;
padding: 20px;
border-radius: 6px;
font-family: 'Consolas', 'Monaco', monospace;
font-size: clamp(0.8rem, 1.5vw, 0.95rem);
line-height: 1.5;
white-space: pre-wrap;
word-wrap: break-word;
overflow-x: auto;
}
.footer {
text-align: center;
padding: 15px;
background: #f8f9fa;
color: #666;
font-size: 0.85rem;
border-top: 1px solid #eee;
}
/* 响应式设计 */
@media (max-width: 768px) {
body {
padding: 10px;
}
.header {
padding: 15px;
}
.info-bar {
flex-direction: column;
align-items: flex-start;
gap: 5px;
}
.log-container {
padding: 15px;
max-height: 60vh;
}
.log-content {
padding: 15px;
font-size: 0.85rem;
}
}
@media (max-width: 480px) {
.header h1 {
font-size: 1.3rem;
}
.header p {
font-size: 0.85rem;
}
.log-content {
font-size: 0.8rem;
padding: 10px;
}
}
/* 滚动条样式 */
.log-container::-webkit-scrollbar {
width: 8px;
}
.log-container::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4px;
}
.log-container::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 4px;
}
.log-container::-webkit-scrollbar-thumb:hover {
background: #a8a8a8;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>📋 程序运行日志</h1>
<p>实时监控系统运行状态</p>
</div>
<div class="info-bar">
<div class="info-item">
<strong>日志文件:</strong> <?php echo htmlspecialchars(basename($logFile)); ?>
</div>
<div class="info-item">
<strong>最后更新:</strong> <?php echo $lastModified; ?>
</div>
<div class="info-item">
<strong>文件大小:</strong> <?php echo formatFileSize(file_exists($logFile) ? filesize($logFile) : 0); ?>
</div>
</div>
<div class="log-container">
<div class="log-content"><?php echo $logContent; ?></div>
</div>
<div class="footer">
<p>© 2026 SecPulse 日志系统 | 自动刷新间隔: 30秒</p>
</div>
</div>
<script>
// 自动刷新功能
setTimeout(function() {
location.reload();
}, 30000); // 30秒自动刷新
// 滚动到底部
window.addEventListener('load', function() {
const logContainer = document.querySelector('.log-container');
if (logContainer) {
logContainer.scrollTop = logContainer.scrollHeight;
}
});
</script>
<?php
// 格式化文件大小函数
function formatFileSize($bytes) {
if ($bytes === 0) return '0 Bytes';
$k = 1024;
$sizes = ['Bytes', 'KB', 'MB', 'GB'];
$i = floor(log($bytes) / log($k));
return round($bytes / pow($k, $i), 2) . ' ' . $sizes[$i];
}
?>
</body>
</html>