gologger/main.go

63 lines
888 B
Go
Raw Normal View History

2023-01-23 10:09:21 +08:00
package gologger
2023-01-23 09:59:30 +08:00
import (
"fmt"
"time"
)
const (
NoLog = iota
Error
Warn
Info
Debug
Trace
)
2023-01-23 10:24:39 +08:00
var loggerMap map[string]Logger
2023-01-23 10:20:17 +08:00
type LogConfig struct {
Name string
Level int
}
2023-01-23 09:59:30 +08:00
type Logger struct {
2023-01-23 10:20:17 +08:00
LogConfig
2023-01-23 09:59:30 +08:00
}
2023-01-23 10:20:17 +08:00
func (l *Logger) log(Level int, msg string) {
2023-01-23 09:59:30 +08:00
now := time.Now()
2023-01-23 10:20:17 +08:00
if Level <= l.Level {
fmt.Println(now.Format("2006-01-02 15:04:05"), " ", l.Name, ": ", msg)
2023-01-23 09:59:30 +08:00
}
}
func (l *Logger) Error(msg string) {
l.log(Error, msg)
}
func (l *Logger) Warn(msg string) {
l.log(Warn, msg)
}
func (l *Logger) Info(msg string) {
l.log(Info, msg)
}
func (l *Logger) Debug(msg string) {
l.log(Debug, msg)
}
func (l *Logger) Trace(msg string) {
l.log(Trace, msg)
}
2023-01-23 10:20:17 +08:00
func GetLogger(logConfig LogConfig) Logger {
2023-01-23 10:24:39 +08:00
if logger, ok := loggerMap[logConfig.Name]; ok {
return logger
} else {
logger = Logger{logConfig}
loggerMap[logConfig.Name] = logger
return logger
}
2023-01-23 09:59:30 +08:00
}