diff --git a/adapter/kratos/log.go b/adapter/kratos/log.go new file mode 100644 index 0000000..6fd5c76 --- /dev/null +++ b/adapter/kratos/log.go @@ -0,0 +1,64 @@ +package logs + +import ( + coopLog "github.com/coopnorge/go-logger" + "github.com/go-kratos/kratos/v2/log" +) + +// Ensure LoggerKratosAdapter implements the log.Logger interface. +var _ log.Logger = (*LoggerKratosAdapter)(nil) + +// LoggerKratosAdapter adapter for Go-Kratos. +type LoggerKratosAdapter struct { + log *coopLog.Logger +} + +// NewLoggerKratosAdapter constructor that accepting Coop logger adapter for Go-Kratos. +// +// Example: +// +// package main +// +// import ( +// "github.com/coopnorge/go-logger" +// "github.com/go-kratos/kratos/v2/log" +// ) +// +// func main() { +// // Create a Coop logger. +// logger.ConfigureGlobalLogger(logger.WithLevel(logger.LevelDebug), logger.WithHook(tracelogger.NewHook())) +// +// // Create a LoggerKratosAdapter and pass it to Go-Kratos so it will know what adapter of logger to use. +// log.SetLogger(logs.NewLoggerKratosAdapter(logger.Global())) +// } +func NewLoggerKratosAdapter(coopLog *coopLog.Logger) *LoggerKratosAdapter { + return &LoggerKratosAdapter{log: coopLog} +} + +// Log prints the keyValPairs to the log. +// +// Example of logs: +// +// {"file":"/project/internal/pkg/logs/kratos.go:36","function":"xxx","level":"debug","msg":"msgconfig loaded: MY_ENV format: ","time":"2000-03-08T10:49:12Z"} +// +// {"file":"/project/internal/pkg/logs/kratos.go:34","function":"xxx","level":"info","msg":"I'm godoc example message","time":"2000-03-08T10:49:12Z"} +func (l *LoggerKratosAdapter) Log(level log.Level, keyValPairs ...interface{}) error { + if len(keyValPairs) == 0 { + return nil + } + + switch level { + case log.LevelFatal: + coopLog.Fatal(keyValPairs...) + case log.LevelError: + coopLog.Error(keyValPairs...) + case log.LevelWarn: + coopLog.Warn(keyValPairs...) + case log.LevelInfo: + coopLog.Info(keyValPairs...) + default: + coopLog.Debug(keyValPairs...) + } + + return nil +} diff --git a/adapter/kratos/log_test.go b/adapter/kratos/log_test.go new file mode 100644 index 0000000..7c21e54 --- /dev/null +++ b/adapter/kratos/log_test.go @@ -0,0 +1,38 @@ +package logs + +import ( + "testing" + + coopLog "github.com/coopnorge/go-logger" + "github.com/go-kratos/kratos/v2/log" + "github.com/stretchr/testify/assert" +) + +func TestTestLoggerKratosAdapterEmptyLog(t *testing.T) { + coopLogger := coopLog.New(coopLog.WithLevel(coopLog.LevelDebug)) + loggerAdapter := NewLoggerKratosAdapter(coopLogger) + + logErr := loggerAdapter.Log(log.LevelWarn) + assert.NoError(t, logErr) +} + +func TestLoggerKratosAdapterLevels(t *testing.T) { + tests := []struct { + level log.Level + }{ + {log.LevelError}, + {log.LevelWarn}, + {log.LevelInfo}, + {log.LevelDebug}, + } + + for _, test := range tests { + t.Run(test.level.String(), func(t *testing.T) { + coopLogger := coopLog.New(coopLog.WithLevel(coopLog.LevelDebug)) + loggerAdapter := NewLoggerKratosAdapter(coopLogger) + + logErr := loggerAdapter.Log(test.level, "test message") + assert.NoError(t, logErr) + }) + } +} diff --git a/go.mod b/go.mod index 6b06ba3..fb12413 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,20 @@ module github.com/coopnorge/go-logger go 1.21 require ( + github.com/go-kratos/kratos/v2 v2.7.2 github.com/labstack/gommon v0.4.2 github.com/sirupsen/logrus v1.9.3 + github.com/stretchr/testify v1.8.4 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/text v0.2.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect golang.org/x/sys v0.15.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 6e84314..048a442 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,13 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 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/go-kratos/kratos/v2 v2.7.2 h1:WVPGFNLKpv+0odMnCPxM4ZHa2hy9I5FOnwpG3Vv4w5c= +github.com/go-kratos/kratos/v2 v2.7.2/go.mod h1:rppuc8+pGL2UtXA29bgFHWKqaaF6b6GB2XIYiDvFBRk= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -10,6 +17,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 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/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= 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= @@ -26,6 +35,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=