From a6aaaa7495ed66144005a06dee1074914419446e Mon Sep 17 00:00:00 2001 From: kingecg Date: Wed, 13 Dec 2023 23:05:42 +0800 Subject: [PATCH] =?UTF-8?q?getlogger=E8=BF=94=E5=9B=9E=E6=8C=87=E9=92=88?= =?UTF-8?q?=EF=BC=8Cfile=20=E6=94=B9=E4=B8=BA=E9=80=9A=E8=BF=87=E5=8D=8F?= =?UTF-8?q?=E7=A8=8B=E5=86=99=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 2 +- console.go | 4 ++-- file.go | 23 ++++++++++++++++++----- main.go | 37 +++++++++++++++++++------------------ main_test.go | 26 ++++++++++++++++++++++++++ test/main.go | 1 + 6 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 main_test.go diff --git a/.vscode/launch.json b/.vscode/launch.json index 2c86451..304593b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "go", "request": "launch", "mode": "auto", - "program": "${fileDirname}", + "program": "${workspaceFolder}/test", "cwd": "${workspaceFolder}", } ] diff --git a/console.go b/console.go index e5bfb65..fabfddb 100644 --- a/console.go +++ b/console.go @@ -35,9 +35,9 @@ func (c *ConsoleAppender) Append(logEvent LogEvent) { fmt.Printf(TraceTemplate, logMsg) } } -func makeConsoleAppender(appenderConfig LogAppenderConfig) LoggerAppender { +func makeConsoleAppender(appenderConfig LogAppenderConfig) *LoggerAppender { var appender LoggerAppender = &ConsoleAppender{} - return appender + return &appender } func init() { RegistAppender("console", makeConsoleAppender) diff --git a/file.go b/file.go index 5d56227..cf023ce 100644 --- a/file.go +++ b/file.go @@ -7,6 +7,7 @@ import ( type FileAppender struct { filePath string + lchan chan LogEvent file *os.File } @@ -14,7 +15,8 @@ func (f *FileAppender) GetName() string { return "FileAppender:" + f.filePath } -func (f *FileAppender) Append(logEvent LogEvent) { +func (f *FileAppender) start() { + f.lchan = make(chan LogEvent, 10) if f.file == nil || int(f.file.Fd()) == -1 { dirName := filepath.Dir(f.filePath) _, err := os.Stat(dirName) @@ -24,12 +26,20 @@ func (f *FileAppender) Append(logEvent LogEvent) { f.file, _ = os.OpenFile(f.filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) } - logMsg := format(logEvent) - f.file.WriteString(logMsg) + go func() { + for { + logEvent := <-f.lchan + logMsg := format(logEvent) + f.file.WriteString(logMsg) + } + }() +} +func (f *FileAppender) Append(logEvent LogEvent) { + f.lchan <- logEvent } -func makeFileAppender(appenderConfig LogAppenderConfig) LoggerAppender { +func makeFileAppender(appenderConfig LogAppenderConfig) *LoggerAppender { var logfile interface{} var ok bool logfile, ok = appenderConfig.Options["file"] @@ -39,7 +49,10 @@ func makeFileAppender(appenderConfig LogAppenderConfig) LoggerAppender { var ret LoggerAppender = &FileAppender{ filePath: logfile.(string), } - return ret + + ret.(*FileAppender).start() + + return &ret } func init() { diff --git a/main.go b/main.go index 13c22cd..ed6aa82 100644 --- a/main.go +++ b/main.go @@ -22,9 +22,9 @@ var logLevelMap map[string]int = map[string]int{ "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{} +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{} var loggerConfig LoggersConfig type LogAppenderConfig struct { @@ -43,7 +43,7 @@ type LoggersConfig struct { type Logger struct { category string level int - appenders []LoggerAppender + appenders []*LoggerAppender } type LogEvent struct { @@ -62,7 +62,7 @@ var consoleAppender LoggerAppender = &ConsoleAppender{} var defaultLogger = &Logger{ level: Error, - appenders: []LoggerAppender{consoleAppender}, + appenders: []*LoggerAppender{&consoleAppender}, } func (l *Logger) log(Level int, msg []interface{}) { @@ -71,7 +71,7 @@ func (l *Logger) log(Level int, msg []interface{}) { now := time.Now() logEvent := LogEvent{l.category, now, Level, msg} for _, appender := range l.appenders { - appender.Append(logEvent) + (*appender).Append(logEvent) } // l.Appender.Append(logEvent) // fmt.Println(now.Format("2006-01-02 15:04:05"), " ", l.Name, ": ", msg) @@ -97,7 +97,7 @@ func (l *Logger) Debug(msg ...interface{}) { func (l *Logger) Trace(msg ...interface{}) { l.log(Trace, msg) } -func GetLogger(name string) Logger { +func GetLogger(name string) *Logger { if logger, ok := loggerMap[name]; ok { return logger } else { @@ -106,29 +106,30 @@ func GetLogger(name string) Logger { return makeLogger(name, logConfig) } if name == "default" { - return *defaultLogger + return defaultLogger } - ret := GetLogger("default") - ret.category = name - return ret + l := *GetLogger("default") + l.category = name + loggerMap[name] = &l + return &l } } -func makeLogger(name string, config LogConfig) Logger { +func makeLogger(name string, config LogConfig) *Logger { logger := &Logger{category: name} levelstr := strings.ToLower(config.Level) logger.level = logLevelMap[levelstr] if config.Appenders == nil || len(config.Appenders) == 0 { - logger.appenders = []LoggerAppender{consoleAppender} + logger.appenders = []*LoggerAppender{&consoleAppender} } else { - logger.appenders = make([]LoggerAppender, len(config.Appenders)) + logger.appenders = make([]*LoggerAppender, len(config.Appenders)) for i, appenderName := range config.Appenders { logger.appenders[i] = appenders[appenderName] } } - loggerMap[name] = *logger - return *logger + loggerMap[name] = logger + return logger } func Configure(config LoggersConfig) { loggerConfig = config @@ -137,7 +138,7 @@ func Configure(config LoggersConfig) { if ok { appenders[name] = appenderFactory(appenderConfig) } else { - appenders[name] = &ConsoleAppender{} + appenders[name] = &consoleAppender } } for name, _ := range loggerConfig.Categories { @@ -145,7 +146,7 @@ func Configure(config LoggersConfig) { } } -func RegistAppender(typeName string, appenderCreatCb func(LogAppenderConfig) LoggerAppender) { +func RegistAppender(typeName string, appenderCreatCb func(LogAppenderConfig) *LoggerAppender) { appenderFactoryMap[typeName] = appenderCreatCb } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..af83f9d --- /dev/null +++ b/main_test.go @@ -0,0 +1,26 @@ +// TestGetLogger tests the GetLogger function +package gologger + +import "testing" + +func TestGetLogger(t *testing.T) { + // Initialize loggerMap and loggerConfig + + dl := GetLogger("default") + if dl != defaultLogger { + t.Errorf("GetLogger(\"defult\") should return defaultLogger") + } + + al := GetLogger("app") + + if al == dl { + t.Errorf("GetLogger(\"app\") should return a new logger") + } + + al2 := GetLogger("app") + + if al2 != al { + t.Errorf("GetLogger(\"app\") should return the same logger") + } + +} diff --git a/test/main.go b/test/main.go index 7a80aa5..89c22bd 100644 --- a/test/main.go +++ b/test/main.go @@ -40,4 +40,5 @@ func main() { defaultLogger.Debug("debug again") fatLogger.Debug("debug again") fatLogger.Error("This is error") + select {} }