From 040e6bc6bf617054c9b74fc8ed4ce7a3b17c80c6 Mon Sep 17 00:00:00 2001 From: Kerwin Date: Thu, 20 Nov 2025 16:05:41 +0800 Subject: [PATCH] update: log --- middleware/logging.go | 27 ++- utils/log_telegram.go | 44 +++- web/components/ShareButtons.vue | 345 -------------------------------- web/package.json | 3 +- web/pages/r/[key].vue | 9 +- web/pnpm-lock.yaml | 21 -- 6 files changed, 52 insertions(+), 397 deletions(-) delete mode 100644 web/components/ShareButtons.vue diff --git a/middleware/logging.go b/middleware/logging.go index c8029c4..dbc5b1e 100644 --- a/middleware/logging.go +++ b/middleware/logging.go @@ -56,22 +56,18 @@ func LoggingMiddleware(next http.Handler) http.Handler { }) } -// logRequest 记录请求日志 - 优化后仅记录异常和关键请求 +// logRequest 记录请求日志 - 恢复正常请求日志记录 func logRequest(r *http.Request, rw *responseWriter, duration time.Duration, requestBody []byte) { // 获取客户端IP clientIP := getClientIP(r) - // 判断是否需要记录日志的条件 - shouldLog := rw.statusCode >= 400 || // 错误状态码 + // 判断是否需要详细记录日志的条件 + shouldDetailLog := rw.statusCode >= 400 || // 错误状态码 duration > 5*time.Second || // 耗时过长 shouldLogPath(r.URL.Path) || // 关键路径 isAdminPath(r.URL.Path) // 管理员路径 - if !shouldLog { - return // 正常请求不记录日志,减少日志噪音 - } - - // 简化的日志格式,移除User-Agent以减少噪音 + // 所有API请求都记录基本信息,但详细日志只记录重要请求 if rw.statusCode >= 400 { // 错误请求记录详细信息 utils.Error("HTTP异常 - %s %s - IP: %s - 状态码: %d - 耗时: %v", @@ -85,10 +81,14 @@ func logRequest(r *http.Request, rw *responseWriter, duration time.Duration, req // 慢请求警告 utils.Warn("HTTP慢请求 - %s %s - IP: %s - 耗时: %v", r.Method, r.URL.Path, clientIP, duration) - } else { + } else if shouldDetailLog { // 关键路径的正常请求 utils.Info("HTTP关键请求 - %s %s - IP: %s - 状态码: %d - 耗时: %v", r.Method, r.URL.Path, clientIP, rw.statusCode, duration) + } else { + // 普通API请求记录简化日志 - 使用Info级别确保能被看到 + // utils.Info("HTTP请求 - %s %s - 状态码: %d - 耗时: %v", + // r.Method, r.URL.Path, rw.statusCode, duration) } } @@ -100,6 +100,13 @@ func shouldLogPath(path string) bool { "/api/admin/config", "/api/admin/users", "/telegram/webhook", + "/api/resources", + "/api/version", + "/api/cks", + "/api/pans", + "/api/categories", + "/api/tags", + "/api/tasks", } for _, keyPath := range keyPaths { @@ -113,7 +120,7 @@ func shouldLogPath(path string) bool { // isAdminPath 判断是否为管理员路径 func isAdminPath(path string) bool { return strings.HasPrefix(path, "/api/admin/") || - strings.HasPrefix(path, "/admin/") + strings.HasPrefix(path, "/admin/") } // getClientIP 获取客户端真实IP地址 diff --git a/utils/log_telegram.go b/utils/log_telegram.go index b0fd08a..7b23771 100644 --- a/utils/log_telegram.go +++ b/utils/log_telegram.go @@ -28,23 +28,40 @@ func GetTelegramLogs(startTime *time.Time, endTime *time.Time, limit int) ([]Tel return []TelegramLogEntry{}, nil } - files, err := filepath.Glob(filepath.Join(logDir, "app_*.log")) + // 查找所有日志文件,包括当前的app.log和历史日志文件 + allFiles, err := filepath.Glob(filepath.Join(logDir, "*.log")) if err != nil { return nil, fmt.Errorf("查找日志文件失败: %v", err) } - if len(files) == 0 { + if len(allFiles) == 0 { return []TelegramLogEntry{}, nil } - // 按时间排序,最近的在前面 - sort.Sort(sort.Reverse(sort.StringSlice(files))) + // 将app.log放在最前面,其他文件按时间排序 + var files []string + var otherFiles []string + + for _, file := range allFiles { + if filepath.Base(file) == "app.log" { + files = append(files, file) // 当前日志文件优先 + } else { + otherFiles = append(otherFiles, file) + } + } + + // 其他文件按时间排序,最近的在前面 + sort.Sort(sort.Reverse(sort.StringSlice(otherFiles))) + files = append(files, otherFiles...) + + // files现在已经是app.log优先,然后是其他文件按时间倒序排列 var allEntries []TelegramLogEntry // 编译Telegram相关的正则表达式 telegramRegex := regexp.MustCompile(`(?i)(\[TELEGRAM.*?\])`) - messageRegex := regexp.MustCompile(`\[(\w+)\]\s+(\d{4}/\d{2}/\d{2}\s+\d{2}:\d{2}:\d{2})\s+\[.*?\]\s+(.*)`) + // 修正正则表达式以匹配实际的日志格式: 2025/01/20 14:30:15 [INFO] [file:line] [TELEGRAM] message + messageRegex := regexp.MustCompile(`(\d{4}/\d{2}/\d{2}\s+\d{2}:\d{2}:\d{2})\s+\[(\w+)\]\s+\[.*?:\d+\]\s+\[TELEGRAM.*?\]\s+(.*)`) for _, file := range files { entries, err := parseTelegramLogsFromFile(file, telegramRegex, messageRegex, startTime, endTime) @@ -119,18 +136,23 @@ func parseTelegramLogsFromFile(filePath string, telegramRegex, messageRegex *reg // parseLogLine 解析单行日志 func parseLogLine(line string, messageRegex *regexp.Regexp) (TelegramLogEntry, error) { - // 匹配日志格式: [LEVEL] 2006/01/02 15:04:05 [file:line] message + // 匹配日志格式: 2006/01/02 15:04:05 [LEVEL] [file:line] [TELEGRAM] message matches := messageRegex.FindStringSubmatch(line) if len(matches) < 4 { return TelegramLogEntry{}, fmt.Errorf("无法解析日志行: %s", line) } - level := matches[1] - timeStr := matches[2] + timeStr := matches[1] + level := matches[2] message := matches[3] - // 解析时间 - timestamp, err := time.Parse("2006/01/02 15:04:05", timeStr) + // 解析时间(使用本地时区) + location, err := time.LoadLocation("Asia/Shanghai") + if err != nil { + return TelegramLogEntry{}, fmt.Errorf("加载时区失败: %v", err) + } + + timestamp, err := time.ParseInLocation("2006/01/02 15:04:05", timeStr, location) if err != nil { return TelegramLogEntry{}, fmt.Errorf("时间解析失败: %v", err) } @@ -203,7 +225,7 @@ func ClearOldTelegramLogs(daysToKeep int) error { return nil // 日志目录不存在,无需清理 } - files, err := filepath.Glob(filepath.Join(logDir, "app_*.log")) + files, err := filepath.Glob(filepath.Join(logDir, "*.log")) if err != nil { return fmt.Errorf("查找日志文件失败: %v", err) } diff --git a/web/components/ShareButtons.vue b/web/components/ShareButtons.vue deleted file mode 100644 index 6358465..0000000 --- a/web/components/ShareButtons.vue +++ /dev/null @@ -1,345 +0,0 @@ - - - - - \ No newline at end of file diff --git a/web/package.json b/web/package.json index d9ed237..9306b42 100644 --- a/web/package.json +++ b/web/package.json @@ -36,8 +36,7 @@ "qr-code-styling": "^1.9.2", "vfonts": "^0.0.3", "vue": "^3.3.0", - "vue-router": "^4.2.0", - "vue-social-share": "^0.0.3" + "vue-router": "^4.2.0" }, "packageManager": "pnpm@9.13.0+sha512.beb9e2a803db336c10c9af682b58ad7181ca0fbd0d4119f2b33d5f2582e96d6c0d93c85b23869295b765170fbdaa92890c0da6ada457415039769edf3c959efe" } diff --git a/web/pages/r/[key].vue b/web/pages/r/[key].vue index 7a8b9d4..545b9b0 100644 --- a/web/pages/r/[key].vue +++ b/web/pages/r/[key].vue @@ -154,14 +154,7 @@ - - - +
diff --git a/web/pnpm-lock.yaml b/web/pnpm-lock.yaml index 73a931c..ce0b58b 100644 --- a/web/pnpm-lock.yaml +++ b/web/pnpm-lock.yaml @@ -50,9 +50,6 @@ importers: vue-router: specifier: ^4.2.0 version: 4.5.1(vue@3.5.18(typescript@5.8.3)) - vue-social-share: - specifier: ^0.0.3 - version: 0.0.3 devDependencies: '@nuxt/devtools': specifier: latest @@ -3916,9 +3913,6 @@ packages: smob@1.5.0: resolution: {integrity: sha512-g6T+p7QO8npa+/hNx9ohv1E5pVCmWrVCUzUXJyLdMmftX6ER0oiWY/w9knEonLpnOp6b6FenKnMfR8gqwWdwig==} - social-share.js@1.0.16: - resolution: {integrity: sha512-NSV6fYFft/U0fEbjXdumZGU3c2oTbnJ6Ha5eNMEEBGsJpD+nu+nbg3LiRygO5GnoNgUa/dOmJyVHb/kM4dJa6g==} - source-map-js@1.2.1: resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==} engines: {node: '>=0.10.0'} @@ -4524,17 +4518,11 @@ packages: vue-devtools-stub@0.1.0: resolution: {integrity: sha512-RutnB7X8c5hjq39NceArgXg28WZtZpGc3+J16ljMiYnFhKvd8hITxSWQSQ5bvldxMDU6gG5mkxl1MTQLXckVSQ==} - vue-github-badge@1.0.1: - resolution: {integrity: sha512-8X+FUWapnnDfs6cRUg3mCfHUf2r5arUfCSRdvbIn860oj9us3Rz3VOtioUgmfzh6EhaaYTs0Oh78EzJ+Z6uqAA==} - vue-router@4.5.1: resolution: {integrity: sha512-ogAF3P97NPm8fJsE4by9dwSYtDwXIY1nFY9T6DyQnGHd1E2Da94w9JIolpe42LJGIl0DwOHBi8TcRPlPGwbTtw==} peerDependencies: vue: ^3.2.0 - vue-social-share@0.0.3: - resolution: {integrity: sha512-zzZGloWVTE/OrEFT0oVfVxzWBvak9KLWiIRWWkPWag10PlGgxTI4o1oN+kXIT+8U3MkRVA8cQLPf5CPqDGmfqw==} - vue@3.5.18: resolution: {integrity: sha512-7W4Y4ZbMiQ3SEo+m9lnoNpV9xG7QVMLa+/0RFwwiAVkeYoyGXqWE85jabU4pllJNUzqfLShJ5YLptewhCWUgNA==} peerDependencies: @@ -8947,8 +8935,6 @@ snapshots: smob@1.5.0: {} - social-share.js@1.0.16: {} - source-map-js@1.2.1: {} source-map-support@0.5.21: @@ -9549,18 +9535,11 @@ snapshots: vue-devtools-stub@0.1.0: {} - vue-github-badge@1.0.1: {} - vue-router@4.5.1(vue@3.5.18(typescript@5.8.3)): dependencies: '@vue/devtools-api': 6.6.4 vue: 3.5.18(typescript@5.8.3) - vue-social-share@0.0.3: - dependencies: - social-share.js: 1.0.16 - vue-github-badge: 1.0.1 - vue@3.5.18(typescript@5.8.3): dependencies: '@vue/compiler-dom': 3.5.18