Skip to content

Commit

Permalink
Implement logger adapter for go-kratos
Browse files Browse the repository at this point in the history
  • Loading branch information
LinMAD committed Mar 8, 2024
1 parent cd3c4b8 commit 3840115
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
41 changes: 41 additions & 0 deletions adapter/kratos/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package logs

import (
"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 *logger.Logger

Check failure on line 13 in adapter/kratos/log.go

View workflow job for this annotation

GitHub Actions / Go 1.21 - validate

undefined: logger (typecheck)
}

// NewLoggerKratosAdapter Coop logger adapter for Go-Kratos.
func NewLoggerKratosAdapter(coopLog *logger.Logger) *LoggerKratosAdapter {

Check failure on line 17 in adapter/kratos/log.go

View workflow job for this annotation

GitHub Actions / Go 1.21 - validate

undefined: logger (typecheck)
return &LoggerKratosAdapter{log: coopLog}
}

// Log print the keyValPairs to log.
func (l *LoggerKratosAdapter) Log(level log.Level, keyValPairs ...interface{}) error {
if len(keyValPairs) == 0 {
return nil
}

switch level {
case log.LevelFatal:
logger.Fatal(keyValPairs...)

Check failure on line 29 in adapter/kratos/log.go

View workflow job for this annotation

GitHub Actions / Go 1.21 - validate

undefined: logger (typecheck)
case log.LevelError:
logger.Error(keyValPairs...)
case log.LevelWarn:
logger.Warn(keyValPairs...)
case log.LevelInfo:
logger.Info(keyValPairs...)
default:
logger.Debug(keyValPairs...)
}

return nil
}
39 changes: 39 additions & 0 deletions adapter/kratos/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package logs

import (
"testing"

"github.com/coopnorge/go-logger"
"github.com/go-kratos/kratos/v2/log"
"github.com/stretchr/testify/assert"
)

func TestTestLoggerKratosAdapterEmptyLog(t *testing.T) {
coopLogger := logger.New(logger.WithLevel(logger.LevelDebug))
loggerAdapter := NewLoggerKratosAdapter(coopLogger)

logErr := loggerAdapter.Log(log.LevelWarn)
assert.NoError(t, logErr)
}

func TestLoggerKratosAdapterLevels(t *testing.T) {
// Test logging levels
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 := logger.New(logger.WithLevel(logger.LevelDebug))
loggerAdapter := NewLoggerKratosAdapter(coopLogger)

logErr := loggerAdapter.Log(test.level, "test message")
assert.NoError(t, logErr)
})
}
}
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand All @@ -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=

0 comments on commit 3840115

Please sign in to comment.