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,
|
|
|
|
}
|
2023-01-23 10:39:10 +08:00
|
|
|
var loggerMap map[string]Logger = map[string]Logger{}
|
2023-11-28 18:29:30 +08:00
|
|
|
var appenderFactoryMap map[string]func(LogAppenderConfig) LoggerAppender = map[string]func(LogAppenderConfig) LoggerAppender{}
|
|
|
|
var appenders map[string]LoggerAppender = map[string]LoggerAppender{}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
// 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)
|
|
|
|
}
|
2023-11-28 18:29:30 +08:00
|
|
|
func GetLogger(name string) Logger {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
ret := GetLogger("default")
|
|
|
|
ret.category = name
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeLogger(name string, config LogConfig) Logger {
|
|
|
|
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}
|
|
|
|
} else {
|
|
|
|
logger.appenders = make([]LoggerAppender, len(config.Appenders))
|
|
|
|
for i, appenderName := range config.Appenders {
|
|
|
|
logger.appenders[i] = appenders[appenderName]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loggerMap[name] = *logger
|
|
|
|
return *logger
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|