Files
cmii-uav-watchdog-project/cmii-uav-watchdog-common/wdd_log/log_utils.go
2025-12-06 11:26:05 +08:00

151 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package wdd_log
import (
"cmii-uav-watchdog-common/utils"
"fmt"
"sync"
)
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
colorPurple = "\033[35m"
colorCyan = "\033[36m"
colorWhite = "\033[37m"
)
type LogLevel int
const (
LevelDebug LogLevel = iota
LevelInfo
LevelWarn
LevelError
LevelFatal
LogPrefix = "cmii-uav-watchdog"
)
var logLevelColors = map[LogLevel]string{
LevelDebug: colorCyan,
LevelInfo: colorGreen,
LevelWarn: colorYellow,
LevelError: colorRed,
LevelFatal: colorPurple,
}
var logLevelNames = map[LogLevel]string{
LevelDebug: "DEBUG",
LevelInfo: "INFO",
LevelWarn: "WARN",
LevelError: "ERROR",
LevelFatal: "FATAL",
}
// Logger 日志单例结构体
type Logger struct {
// EnableDebug 是否启用Debug日志
EnableDebug bool
}
var (
instance *Logger
once sync.Once
)
// GetInstance 获取Logger单例实例
// 返回Logger的单例对象指针
func GetInstance() *Logger {
once.Do(func() {
instance = &Logger{
EnableDebug: false, // 默认不启用Debug日志
}
})
return instance
}
// SetEnableDebug 设置是否启用Debug日志
// enable: true表示启用Debug日志false表示禁用
func (l *Logger) SetEnableDebug(enable bool) {
l.EnableDebug = enable
}
// Log 记录指定级别的日志
// level: 日志级别
// format: 日志格式
// args: 格式化参数
func (l *Logger) Log(level LogLevel, format string, args ...interface{}) {
// Debug级别日志在未启用时不输出
if level == LevelDebug && !l.EnableDebug {
return
}
now := utils.CurentTimeString()
color := logLevelColors[level]
levelName := logLevelNames[level]
message := fmt.Sprintf(format, args...)
fmt.Printf("[%s] %s %s%s%s %s\n", LogPrefix, now, color, levelName, colorReset, message)
}
// Debug 记录Debug级别日志
// format: 日志格式
// args: 格式化参数
func (l *Logger) Debug(format string, args ...interface{}) {
l.Log(LevelDebug, format, args...)
}
// Info 记录Info级别日志
// format: 日志格式
// args: 格式化参数
func (l *Logger) Info(format string, args ...interface{}) {
l.Log(LevelInfo, format, args...)
}
// Warn 记录Warn级别日志
// format: 日志格式
// args: 格式化参数
func (l *Logger) Warn(format string, args ...interface{}) {
l.Log(LevelWarn, format, args...)
}
// Error 记录Error级别日志
// format: 日志格式
// args: 格式化参数
func (l *Logger) Error(format string, args ...interface{}) {
l.Log(LevelError, format, args...)
}
// Fatal 记录Fatal级别日志
// format: 日志格式
// args: 格式化参数
func (l *Logger) Fatal(format string, args ...interface{}) {
l.Log(LevelFatal, format, args...)
}
// 为了兼容原有代码,保留全局函数,但内部调用单例实例
func Log(level LogLevel, format string, args ...interface{}) {
GetInstance().Log(level, format, args...)
}
func Debug(format string, args ...interface{}) {
GetInstance().Debug(format, args...)
}
func Info(format string, args ...interface{}) {
GetInstance().Info(format, args...)
}
func Warn(format string, args ...interface{}) {
GetInstance().Warn(format, args...)
}
func Error(format string, args ...interface{}) {
GetInstance().Error(format, args...)
}
func Fatal(format string, args ...interface{}) {
GetInstance().Fatal(format, args...)
}