gologger/main.go

154 lines
3.3 KiB
Go
Raw Permalink Normal View History

2023-01-23 10:09:21 +08:00
package gologger
2023-01-23 09:59:30 +08:00
import (
2023-11-28 18:29:30 +08:00
"strings"
2023-01-23 09:59:30 +08:00
"time"
)
const (
NoLog = iota
Error
Warn
Info
Debug
Trace
)
2023-11-28 18:29:30 +08:00
var logLevelMap map[string]int = map[string]int{
"off": NoLog,
"error": Error,
"warn": Warn,
"info": Info,
"debug": Debug,
"trace": Trace,
}
var loggerMap map[string]*Logger = map[string]*Logger{}
var appenderFactoryMap map[string]func(LogAppenderConfig) *LoggerAppender = map[string]func(LogAppenderConfig) *LoggerAppender{}
var appenders map[string]*LoggerAppender = map[string]*LoggerAppender{}
2023-11-28 18:29:30 +08:00
var loggerConfig LoggersConfig
2023-01-23 10:24:39 +08:00
2023-11-28 18:29:30 +08:00
type LogAppenderConfig struct {
Type string `json:"type"`
Options map[string]interface{} `json:"options"`
}
2023-01-23 10:20:17 +08:00
type LogConfig struct {
2023-11-28 18:29:30 +08:00
Level string `json:"level"`
Appenders []string `json:"appenders"`
}
type LoggersConfig struct {
Appenders map[string]LogAppenderConfig `json:"appenders"`
Categories map[string]LogConfig `json:"categories"`
2023-01-23 10:20:17 +08:00
}
2023-01-23 09:59:30 +08:00
type Logger struct {
2023-11-28 18:29:30 +08:00
category string
level int
appenders []*LoggerAppender
2023-01-23 09:59:30 +08:00
}
2023-01-23 11:35:44 +08:00
type LogEvent struct {
2023-11-28 18:29:30 +08:00
Category string
Ts time.Time
Level int
Data []interface{}
2023-01-23 11:35:44 +08:00
}
type LoggerAppender interface {
GetName() string
Append(logEvent LogEvent)
2024-09-21 14:25:45 +08:00
Close()
2023-01-23 11:35:44 +08:00
}
var consoleAppender LoggerAppender = &ConsoleAppender{}
2023-11-28 18:29:30 +08:00
var defaultLogger = &Logger{
level: Error,
appenders: []*LoggerAppender{&consoleAppender},
2023-01-23 11:35:44 +08:00
}
2023-01-23 11:48:03 +08:00
func (l *Logger) log(Level int, msg []interface{}) {
2023-01-23 11:35:44 +08:00
2023-11-28 18:29:30 +08:00
if Level <= l.level {
2023-01-23 11:35:44 +08:00
now := time.Now()
2023-11-28 18:29:30 +08:00
logEvent := LogEvent{l.category, now, Level, msg}
for _, appender := range l.appenders {
(*appender).Append(logEvent)
2023-11-28 18:29:30 +08:00
}
// l.Appender.Append(logEvent)
2023-01-23 11:35:44 +08:00
// fmt.Println(now.Format("2006-01-02 15:04:05"), " ", l.Name, ": ", msg)
2023-01-23 09:59:30 +08:00
}
}
2023-01-23 11:35:44 +08:00
func (l *Logger) Error(msg ...interface{}) {
2023-01-23 09:59:30 +08:00
l.log(Error, msg)
}
2023-01-23 11:35:44 +08:00
func (l *Logger) Warn(msg ...interface{}) {
2023-01-23 09:59:30 +08:00
l.log(Warn, msg)
}
2023-01-23 11:35:44 +08:00
func (l *Logger) Info(msg ...interface{}) {
2023-01-23 09:59:30 +08:00
l.log(Info, msg)
}
2023-01-23 11:35:44 +08:00
func (l *Logger) Debug(msg ...interface{}) {
2023-01-23 09:59:30 +08:00
l.log(Debug, msg)
}
2023-01-23 11:35:44 +08:00
func (l *Logger) Trace(msg ...interface{}) {
2023-01-23 09:59:30 +08:00
l.log(Trace, msg)
}
func GetLogger(name string) *Logger {
2023-11-28 18:29:30 +08:00
if logger, ok := loggerMap[name]; ok {
2023-01-23 10:24:39 +08:00
return logger
} else {
2023-11-28 18:29:30 +08:00
logConfig, ok := loggerConfig.Categories[name]
if ok {
return makeLogger(name, logConfig)
}
if name == "default" {
return defaultLogger
2023-11-28 18:29:30 +08:00
}
l := *GetLogger("default")
l.category = name
loggerMap[name] = &l
return &l
2023-11-28 18:29:30 +08:00
}
}
func makeLogger(name string, config LogConfig) *Logger {
2023-11-28 18:29:30 +08:00
logger := &Logger{category: name}
levelstr := strings.ToLower(config.Level)
logger.level = logLevelMap[levelstr]
2023-11-29 10:16:56 +08:00
2023-11-28 18:29:30 +08:00
if config.Appenders == nil || len(config.Appenders) == 0 {
logger.appenders = []*LoggerAppender{&consoleAppender}
2023-11-28 18:29:30 +08:00
} else {
logger.appenders = make([]*LoggerAppender, len(config.Appenders))
2023-11-28 18:29:30 +08:00
for i, appenderName := range config.Appenders {
logger.appenders[i] = appenders[appenderName]
}
}
loggerMap[name] = logger
return logger
2023-11-28 18:29:30 +08:00
}
func Configure(config LoggersConfig) {
loggerConfig = config
for name, appenderConfig := range loggerConfig.Appenders {
appenderFactory, ok := appenderFactoryMap[appenderConfig.Type]
if ok {
appenders[name] = appenderFactory(appenderConfig)
} else {
appenders[name] = &consoleAppender
2023-01-23 11:35:44 +08:00
}
2023-01-23 10:24:39 +08:00
}
2023-11-28 18:29:30 +08:00
for name, _ := range loggerConfig.Categories {
GetLogger(name)
}
}
func RegistAppender(typeName string, appenderCreatCb func(LogAppenderConfig) *LoggerAppender) {
2023-01-23 10:24:39 +08:00
2023-11-28 18:29:30 +08:00
appenderFactoryMap[typeName] = appenderCreatCb
2023-01-23 09:59:30 +08:00
}