From 0b00522066778b835c2564fee37bbc194591d89a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Chuazhongming=E2=80=9D?= Date: Mon, 16 Dec 2024 22:22:19 +0800 Subject: [PATCH] Change to mosn logger api MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: “huazhongming” --- go.mod | 4 +- go.sum | 6 +- kit/go.mod | 14 ++++- kit/go.sum | 21 +++++++ kit/logger/logger.go | 137 +++++++++++++++++++++++++++---------------- 5 files changed, 125 insertions(+), 57 deletions(-) diff --git a/go.mod b/go.mod index 63dafbb168..3e68dd26bc 100644 --- a/go.mod +++ b/go.mod @@ -38,9 +38,10 @@ require ( k8s.io/apimachinery v0.20.2 mosn.io/api v1.5.0 mosn.io/layotto/components v0.0.0-20230712052228-c940b7f1367d + mosn.io/layotto/kit v0.0.0-00010101000000-000000000000 mosn.io/layotto/spec v0.0.0-20231023045845-48ec2bc7eab8 mosn.io/mosn v1.5.1-0.20230529091910-7d48a20e544b - mosn.io/pkg v1.5.1-0.20230525074748-e3528eae50d9 + mosn.io/pkg v1.6.0 mosn.io/proxy-wasm-go-host v0.2.1-0.20230303015731-d5007ceaba4f ) @@ -315,7 +316,6 @@ require ( k8s.io/client-go v0.20.0 // indirect k8s.io/klog/v2 v2.4.0 // indirect k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect - mosn.io/layotto/kit v0.0.0-00010101000000-000000000000 // indirect nhooyr.io/websocket v1.8.7 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.0.2 // indirect sigs.k8s.io/yaml v1.2.0 // indirect diff --git a/go.sum b/go.sum index 2aeda64b45..aa46b6800a 100644 --- a/go.sum +++ b/go.sum @@ -107,8 +107,8 @@ github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZ github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk= github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo= github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU= -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= @@ -2044,8 +2044,8 @@ mosn.io/api v1.5.0 h1:Y9s6NHJx0etcqIDDP7XeoTfgceDFMBnrZphxqDsxWOE= mosn.io/api v1.5.0/go.mod h1:mJX2oRJkrXjLN6hY1Wwrlxj0F+RqEPOMhbf2WhZO+VY= mosn.io/mosn v1.5.1-0.20230529091910-7d48a20e544b h1:ceGtIyU+IFwcB+Vj3bHMTl+8HDcP8slhy7h5oHYpSVg= mosn.io/mosn v1.5.1-0.20230529091910-7d48a20e544b/go.mod h1:+4vh9qHhB+hjUgy/ES13DJ85xtHQWaYAmbHB558Yzvw= -mosn.io/pkg v1.5.1-0.20230525074748-e3528eae50d9 h1:uKHek5nTFAAkqBGpQhYG2Jhz3k0aoqJiNHKCAUNYNCs= -mosn.io/pkg v1.5.1-0.20230525074748-e3528eae50d9/go.mod h1:/EptiefKMKBRvrveNPYEAAgthCTSme52sLMlBXBIBm8= +mosn.io/pkg v1.6.0 h1:R+T344PEp7CauQvXEitDJTXQ0bIeOhLwnaey9qwN4Fs= +mosn.io/pkg v1.6.0/go.mod h1:/EptiefKMKBRvrveNPYEAAgthCTSme52sLMlBXBIBm8= nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= diff --git a/kit/go.mod b/kit/go.mod index fc4f2d233f..1e6b087de2 100644 --- a/kit/go.mod +++ b/kit/go.mod @@ -2,6 +2,16 @@ module mosn.io/layotto/kit go 1.18 -require github.com/sirupsen/logrus v1.9.3 +require ( + mosn.io/pkg v1.6.0 +) -require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect +require ( + github.com/BurntSushi/toml v1.4.0 // indirect + github.com/golang/protobuf v1.5.0 // indirect + github.com/hashicorp/go-syslog v1.0.0 // indirect + github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect + google.golang.org/protobuf v1.26.0-rc.1 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect + mosn.io/api v1.5.0 // indirect +) diff --git a/kit/go.sum b/kit/go.sum index 21f9bfb372..8b7dff7790 100644 --- a/kit/go.sum +++ b/kit/go.sum @@ -1,8 +1,18 @@ +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/hashicorp/go-syslog v1.0.0 h1:KaodqZuhUoZereWVIYmpUgZysurB1kBLX2j0MwMrUAE= +github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 h1:MkV+77GLUNo5oJ0jf870itWm3D0Sjh7+Za9gazKc5LQ= +github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -10,6 +20,17 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1 h1:7QnIQpGRHE5RnLKnESfDoxm2dTapTZua5a0kS0A+VXQ= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8= +gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +mosn.io/api v1.5.0 h1:Y9s6NHJx0etcqIDDP7XeoTfgceDFMBnrZphxqDsxWOE= +mosn.io/api v1.5.0/go.mod h1:mJX2oRJkrXjLN6hY1Wwrlxj0F+RqEPOMhbf2WhZO+VY= +mosn.io/pkg v1.6.0 h1:R+T344PEp7CauQvXEitDJTXQ0bIeOhLwnaey9qwN4Fs= +mosn.io/pkg v1.6.0/go.mod h1:/EptiefKMKBRvrveNPYEAAgthCTSme52sLMlBXBIBm8= diff --git a/kit/logger/logger.go b/kit/logger/logger.go index 6381899419..928685ba45 100644 --- a/kit/logger/logger.go +++ b/kit/logger/logger.go @@ -17,11 +17,11 @@ package logger import ( - "os" + "fmt" "strings" "sync" - "github.com/sirupsen/logrus" + "mosn.io/pkg/log" ) const ( @@ -41,8 +41,6 @@ const ( // UndefinedLevel is for undefined log level. UndefinedLevel LogLevel = "undefined" - logFieldComponent = "component" - logKeyDebug = "debug" logKeyAccess = "access" logKeyError = "error" @@ -75,18 +73,22 @@ func SetComponentLoggerLevel(componentName string, level string) { logLevel := toLogLevel(level) logger, ok := loggerListeners.Load(componentName) if !ok { - logrus.Warnf("component logger for %s not found", componentName) - } - componentLoggerListener, ok := logger.(ComponentLoggerListener) - if !ok { - logrus.Warnf("component logger for %s is not ComponentLoggerListener", componentName) + log.DefaultLogger.Warnf("component logger for %s not found", componentName) + } else { + componentLoggerListener, ok := logger.(ComponentLoggerListener) + if !ok { + log.DefaultLogger.Warnf("component logger for %s is not ComponentLoggerListener", componentName) + } else { + componentLoggerListener.OnLogLevelChanged(logLevel) + } } - componentLoggerListener.OnLogLevelChanged(logLevel) } // SetDefaultLoggerLevel sets the default log output level. func SetDefaultLoggerLevel(level string) { - defaultLoggerLevel = toLogLevel(level) + if level != "" { + defaultLoggerLevel = toLogLevel(level) + } } // SetDefaultLoggerFilePath sets the default log file path. @@ -101,7 +103,7 @@ type layottoLogger struct { logLevel LogLevel - loggers map[string]*logrus.Entry + loggers map[string]log.ErrorLogger } // Logger api for logging. @@ -180,97 +182,111 @@ func NewLayottoLogger(name string) Logger { ll := &layottoLogger{ name: name, logLevel: defaultLoggerLevel, - loggers: make(map[string]*logrus.Entry), + loggers: make(map[string]log.ErrorLogger), } - dLogger := logrus.New() - dFile, err := os.OpenFile(defaultLogFilePath+fileNameDebug, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + dMosnLogger, err := log.GetOrCreateLogger(defaultLogFilePath+fileNameDebug, nil) + + dLogger := &log.SimpleErrorLog{ + Logger: dMosnLogger, + Level: log.DEBUG, + } if err != nil { - logrus.Fatalf("Failed to open log file: %v", err) + ll.loggers[logKeyDebug] = log.DefaultLogger + log.DefaultLogger.Fatalf("Failed to create mosn logger: %v", err) + } else { + dLogger.SetLogLevel(toMosnLoggerLevel(defaultLoggerLevel)) + ll.loggers[logKeyDebug] = dLogger } - dLogger.SetLevel(toLogrusLevel(defaultLoggerLevel)) - dLogger.SetOutput(dFile) - ll.loggers[logKeyDebug] = dLogger.WithField(logFieldComponent, name) - aLogger := logrus.New() - aFile, err := os.OpenFile(defaultLogFilePath+fileNameAccess, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + aMosnLogger, err := log.GetOrCreateLogger(defaultLogFilePath+fileNameAccess, nil) + + aLogger := &log.SimpleErrorLog{ + Logger: aMosnLogger, + Level: log.INFO, + } if err != nil { - logrus.Fatalf("Failed to open log file: %v", err) + ll.loggers[logKeyAccess] = log.DefaultLogger + log.DefaultLogger.Fatalf("Failed to create mosn logger: %v", err) + } else { + aLogger.SetLogLevel(toMosnLoggerLevel(defaultLoggerLevel)) + ll.loggers[logKeyAccess] = aLogger } - aLogger.SetLevel(toLogrusLevel(defaultLoggerLevel)) - aLogger.SetOutput(aFile) - ll.loggers[logKeyAccess] = aLogger.WithField(logFieldComponent, name) - eLogger := logrus.New() - eFile, err := os.OpenFile(defaultLogFilePath+fileNameError, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + eMosnLogger, err := log.GetOrCreateLogger(defaultLogFilePath+fileNameError, nil) + + eLogger := &log.SimpleErrorLog{ + Logger: eMosnLogger, + Level: log.ERROR, + } if err != nil { - logrus.Fatalf("Failed to open log file: %v", err) + ll.loggers[logKeyError] = log.DefaultLogger + log.DefaultLogger.Fatalf("Failed to create mosn logger: %v", err) + } else { + eLogger.SetLogLevel(toMosnLoggerLevel(defaultLoggerLevel)) + ll.loggers[logKeyError] = eLogger } - eLogger.SetLevel(toLogrusLevel(defaultLoggerLevel)) - eLogger.SetOutput(eFile) - ll.loggers[logKeyError] = eLogger.WithField(logFieldComponent, name) - return ll } // Tracef logs a message at level Trace. func (l *layottoLogger) Tracef(format string, args ...interface{}) { - l.loggers[logKeyDebug].Tracef(format, args...) + l.loggers[logKeyDebug].Tracef("[%s] %s", l.name, fmt.Sprintf(format, args...)) } // Trace logs a message at level Trace. func (l *layottoLogger) Trace(args ...interface{}) { - l.loggers[logKeyDebug].Trace(args...) + l.loggers[logKeyDebug].Tracef("%s", args...) } // Debugf logs a message at level Debug. func (l *layottoLogger) Debugf(format string, args ...interface{}) { - l.loggers[logKeyDebug].Debugf(format, args...) + l.loggers[logKeyDebug].Debugf("[%s] %s", l.name, fmt.Sprintf(format, args...)) } // Debug logs a message at level Debug. func (l *layottoLogger) Debug(args ...interface{}) { - l.loggers[logKeyDebug].Debug(args...) + l.loggers[logKeyDebug].Debugf("%s", args...) } // Infof logs a message at level Info. func (l *layottoLogger) Infof(format string, args ...interface{}) { - l.loggers[logKeyAccess].Infof(format, args...) + l.loggers[logKeyAccess].Infof("[%s] %s", l.name, fmt.Sprintf(format, args...)) } // Info logs a message at level Info. func (l *layottoLogger) Info(args ...interface{}) { - l.loggers[logKeyAccess].Info(args...) + l.loggers[logKeyAccess].Infof("%s", args...) } // Warnf logs a message at level Warn. func (l *layottoLogger) Warnf(format string, args ...interface{}) { - l.loggers[logKeyAccess].Warnf(format, args...) + l.loggers[logKeyAccess].Warnf("[%s] %s", l.name, fmt.Sprintf(format, args...)) } // Warn logs a message at level Warn. func (l *layottoLogger) Warn(args ...interface{}) { - l.loggers[logKeyAccess].Warn(args...) + l.loggers[logKeyAccess].Warnf("%s", args...) } // Errorf logs a message at level Error. func (l *layottoLogger) Errorf(format string, args ...interface{}) { - l.loggers[logKeyError].Errorf(format, args...) + l.loggers[logKeyError].Errorf("[%s] %s", l.name, fmt.Sprintf(format, args...)) } // Error logs a message at level Error. func (l *layottoLogger) Error(args ...interface{}) { - l.loggers[logKeyError].Error(args...) + l.loggers[logKeyError].Errorf("%s", args...) } // Fatalf logs a message at level Fatal. func (l *layottoLogger) Fatalf(format string, args ...interface{}) { - l.loggers[logKeyError].Fatalf(format, args...) + l.loggers[logKeyError].Fatalf("[%s] %s", l.name, fmt.Sprintf(format, args...)) } // Fatal logs a message at level Fatal. func (l *layottoLogger) Fatal(args ...interface{}) { - l.loggers[logKeyError].Fatal(args...) + l.loggers[logKeyError].Fatalf("%s", args...) } // GetLogLevel gets the log output level. @@ -278,17 +294,38 @@ func (l *layottoLogger) GetLogLevel() LogLevel { return l.logLevel } -// toLogrusLevel converts to logrus.Level. -func toLogrusLevel(lvl LogLevel) logrus.Level { +// toMosnLoggerLevel converts to logrus.Level. +func toMosnLoggerLevel(lvl LogLevel) log.Level { // ignore error because it will never happen - l, _ := logrus.ParseLevel(string(lvl)) + l, _ := parseLevel(string(lvl)) return l } +// parseLevel takes a string level and returns the Mosn logger level constant. +func parseLevel(lvl string) (log.Level, error) { + switch strings.ToLower(lvl) { + case "fatal": + return log.FATAL, nil + case "error": + return log.ERROR, nil + case "warn", "warning": + return log.WARN, nil + case "info": + return log.INFO, nil + case "debug": + return log.DEBUG, nil + case "trace": + return log.TRACE, nil + } + + var l log.Level + return l, fmt.Errorf("not a valid mosn Level: %q", lvl) +} + // SetLogLevel sets log output level. func (l *layottoLogger) SetLogLevel(outputLevel LogLevel) { l.logLevel = outputLevel - l.loggers[logKeyDebug].Logger.SetLevel(toLogrusLevel(outputLevel)) - l.loggers[logKeyAccess].Logger.SetLevel(toLogrusLevel(outputLevel)) - l.loggers[logKeyError].Logger.SetLevel(toLogrusLevel(outputLevel)) + l.loggers[logKeyDebug].SetLogLevel(toMosnLoggerLevel(outputLevel)) + l.loggers[logKeyAccess].SetLogLevel(toMosnLoggerLevel(outputLevel)) + l.loggers[logKeyError].SetLogLevel(toMosnLoggerLevel(outputLevel)) }