最简单的方式实现 Golang的级别日志
log.go
package log import ( "fmt" "io" "log" "os" ) const ( Ldate = 1 << iota // the date in the local time zone: 2009/01/23 Ltime // the time in the local time zone: 01:23:23 Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime. Llongfile // full file name and line number: /a/b/c/d.go:23 Lshortfile // final file name element and line number: d.go:23. overrides Llongfile LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone Lmsgprefix // move the "prefix" from the beginning of the line to before the message LstdFlags = Ldate | Ltime // initial values for the standard logger ) const ( TRACE int = iota DEBUG INFO WARN ERROR FATAL ) var l *LevelLogger type LevelLogger struct { level int stdout bool logger *log.Logger } type logWriter struct { Writer io.Writer } func newLogWriter(writer io.Writer) *logWriter { return &logWriter{writer} } func (w *logWriter) Write(data []byte) (int, error) { if l.stdout { fmt.Print(string(data)) } return w.Writer.Write(data) } func init() { l = &LevelLogger{ level: TRACE, logger: log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile), } } func SetLevel(level int) { l.level = level } func SetLogger(logger *log.Logger) { l.logger = logger } func SetFlags(flag int) { l.logger.SetFlags(flag) } func SetOutFile(filepath string, stdout bool) { l.stdout = stdout filePtr, err := os.OpenFile(filepath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666) if err != nil { log.Panic(err) } l.logger.SetOutput(newLogWriter(filePtr)) } func SetPrefix(prefix string) { l.logger.SetPrefix(prefix) } func SetOutput(w io.Writer) { l.logger.SetOutput(w) } func output(v ...any) { err := l.logger.Output(3, fmt.Sprintln(v...)) if err != nil { fmt.Println(err) } } func Trace(v ...any) { if TRACE >= l.level { output(fmt.Sprintf("[PID:%d]", os.Getpid()), "[TRACE]", fmt.Sprint(v...)) } } func Debug(v ...any) { if DEBUG >= l.level { output(fmt.Sprintf("[PID:%d]", os.Getpid()), "[DEBUG]", fmt.Sprint(v...)) } } func Info(v ...any) { if INFO >= l.level { output(fmt.Sprintf("[PID:%d]", os.Getpid()), "[INFO]", fmt.Sprint(v...)) } } func Warn(v ...any) { if WARN >= l.level { output(fmt.Sprintf("[PID:%d]", os.Getpid()), "[WARN]", fmt.Sprint(v...)) } } func Error(v ...any) { if ERROR >= l.level { output(fmt.Sprintf("[PID:%d]", os.Getpid()), "[ERROR]", fmt.Sprint(v...)) } } func Fatal(v ...any) { if FATAL >= l.level { output(fmt.Sprintf("[PID:%d]", os.Getpid()), "[FATAL]", fmt.Sprint(v...)) os.Exit(0) } } func Println(v ...any) { fmt.Println(v...) }
main.go
package main import ( "awesomeProject1/log" ) func main() { log.SetLevel(log.INFO) log.SetOutFile("test.log", true) log.Trace("the is Trace") log.Debug("the is Debug") log.Info("the is Info") log.Warn("the is Warn") log.Error("the is Error") log.Fatal("the is Fatal", 1, 2, 3, 4, 5, 6, 7, 8, 9) }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话