36d089705a
禁用 log 默认时间戳,避免日志输出重复时间 Co-Authored-By: AI
150 lines
3.1 KiB
Go
150 lines
3.1 KiB
Go
package logger
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
// 日志级别
|
|
level = "info"
|
|
mu sync.Mutex
|
|
)
|
|
|
|
func init() {
|
|
// 禁用 log 默认的时间戳前缀
|
|
log.SetFlags(0)
|
|
}
|
|
|
|
// SetLevel 设置日志级别
|
|
func SetLevel(l string) {
|
|
mu.Lock()
|
|
level = l
|
|
mu.Unlock()
|
|
}
|
|
|
|
// 日志格式:[时间] [级别] 消息
|
|
func formatLog(levelStr, msg string) string {
|
|
return fmt.Sprintf("[%s] [%s] %s", time.Now().Format("2006-01-02 15:04:05"), levelStr, msg)
|
|
}
|
|
|
|
// Info 输出信息日志
|
|
func Info(msg string) {
|
|
log.Println(formatLog("INFO", msg))
|
|
}
|
|
|
|
// Infof 输出格式化信息日志
|
|
func Infof(format string, args ...interface{}) {
|
|
Info(fmt.Sprintf(format, args...))
|
|
}
|
|
|
|
// Warn 输出警告日志
|
|
func Warn(msg string) {
|
|
log.Println(formatLog("WARN", msg))
|
|
}
|
|
|
|
// Warnf 输出格式化警告日志
|
|
func Warnf(format string, args ...interface{}) {
|
|
Warn(fmt.Sprintf(format, args...))
|
|
}
|
|
|
|
// Error 输出错误日志
|
|
func Error(msg string) {
|
|
log.Println(formatLog("ERROR", msg))
|
|
}
|
|
|
|
// Errorf 输出格式化错误日志
|
|
func Errorf(format string, args ...interface{}) {
|
|
Error(fmt.Sprintf(format, args...))
|
|
}
|
|
|
|
// Debug 输出调试日志(仅在 debug 别别时输出)
|
|
func Debug(msg string) {
|
|
mu.Lock()
|
|
currentLevel := level
|
|
mu.Unlock()
|
|
if currentLevel == "debug" {
|
|
log.Println(formatLog("DEBUG", msg))
|
|
}
|
|
}
|
|
|
|
// Debugf 输出格式化调试日志
|
|
func Debugf(format string, args ...interface{}) {
|
|
Debug(fmt.Sprintf(format, args...))
|
|
}
|
|
|
|
// Fatal 输出致命错误日志并退出
|
|
func Fatal(msg string) {
|
|
log.Println(formatLog("FATAL", msg))
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Fatalf 输出格式化致命错误日志并退出
|
|
func Fatalf(format string, args ...interface{}) {
|
|
Fatal(fmt.Sprintf(format, args...))
|
|
}
|
|
|
|
// Task 相关日志
|
|
|
|
// TaskCreated 任务创建日志
|
|
func TaskCreated(taskID int64, title string, bvid string) {
|
|
Infof("任务创建 #%d: %s (%s)", taskID, title, bvid)
|
|
}
|
|
|
|
// TaskStarted 任务开始日志
|
|
func TaskStarted(taskID int64, title string) {
|
|
Infof("任务开始 #%d: %s", taskID, title)
|
|
}
|
|
|
|
// TaskCompleted 任务完成日志
|
|
func TaskCompleted(taskID int64, title string, duration time.Duration) {
|
|
Infof("任务完成 #%d: %s (耗时 %.1f秒)", taskID, title, duration.Seconds())
|
|
}
|
|
|
|
// TaskFailed 任务失败日志
|
|
func TaskFailed(taskID int64, title string, err error) {
|
|
Errorf("任务失败 #%d: %s - %v", taskID, title, err)
|
|
}
|
|
|
|
// TaskCancelled 任务取消日志
|
|
func TaskCancelled(taskID int64, title string) {
|
|
Warnf("任务取消 #%d: %s", taskID, title)
|
|
}
|
|
|
|
// User 用户相关日志
|
|
|
|
// UserLogin 用户登录日志
|
|
func UserLogin() {
|
|
Info("用户登录成功")
|
|
}
|
|
|
|
// UserLogout 用户登出日志
|
|
func UserLogout() {
|
|
Info("用户登出")
|
|
}
|
|
|
|
// Server 服务相关日志
|
|
|
|
// ServerStarted 服务启动日志
|
|
func ServerStarted(port int, version string) {
|
|
Infof("服务启动 %s,端口 %d", version, port)
|
|
}
|
|
|
|
// FFmpegStatus FFmpeg 状态日志
|
|
func FFmpegStatus(available bool) {
|
|
if available {
|
|
Info("FFmpeg 检测成功")
|
|
} else {
|
|
Warn("FFmpeg 未安装,视频下载功能不可用")
|
|
}
|
|
}
|
|
|
|
// API API 相关日志
|
|
|
|
// APICall API 调用日志
|
|
func APICall(method, path string) {
|
|
Debugf("API %s %s", method, path)
|
|
} |