86 lines
1.4 KiB
Go
86 lines
1.4 KiB
Go
package gologger
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
NoLog = iota
|
|
Error
|
|
Warn
|
|
Info
|
|
Debug
|
|
Trace
|
|
)
|
|
|
|
var loggerMap map[string]Logger = map[string]Logger{}
|
|
|
|
type LogConfig struct {
|
|
Name string
|
|
Level int
|
|
Appender LoggerAppender
|
|
}
|
|
type Logger struct {
|
|
LogConfig
|
|
}
|
|
|
|
type LogEvent struct {
|
|
Ts time.Time
|
|
Level int
|
|
Data []interface{}
|
|
}
|
|
|
|
type LoggerAppender interface {
|
|
GetName() string
|
|
Append(logEvent LogEvent)
|
|
}
|
|
|
|
var consoleAppender LoggerAppender = &ConsoleAppender{}
|
|
var defaultLogger = &LogConfig{
|
|
Level: Error,
|
|
Appender: consoleAppender,
|
|
}
|
|
|
|
func (l *Logger) log(Level int, msg []interface{}) {
|
|
|
|
if Level <= l.Level {
|
|
now := time.Now()
|
|
logEvent := LogEvent{now, Level, msg}
|
|
l.Appender.Append(logEvent)
|
|
// fmt.Println(now.Format("2006-01-02 15:04:05"), " ", l.Name, ": ", msg)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Error(msg ...interface{}) {
|
|
l.log(Error, msg)
|
|
}
|
|
|
|
func (l *Logger) Warn(msg ...interface{}) {
|
|
l.log(Warn, msg)
|
|
}
|
|
|
|
func (l *Logger) Info(msg ...interface{}) {
|
|
l.log(Info, msg)
|
|
}
|
|
|
|
func (l *Logger) Debug(msg ...interface{}) {
|
|
l.log(Debug, msg)
|
|
}
|
|
|
|
func (l *Logger) Trace(msg ...interface{}) {
|
|
l.log(Trace, msg)
|
|
}
|
|
func GetLogger(logConfig LogConfig) Logger {
|
|
if logger, ok := loggerMap[logConfig.Name]; ok {
|
|
return logger
|
|
} else {
|
|
logger := &Logger{logConfig}
|
|
if logger.Appender == nil {
|
|
logger.Appender = defaultLogger.Appender
|
|
}
|
|
loggerMap[logConfig.Name] = *logger
|
|
return *logger
|
|
}
|
|
|
|
}
|