Skip to content

Commit

Permalink
refactor(log): Refactored manager logger to logging library. (#72)
Browse files Browse the repository at this point in the history
* refactor(log): Refactored manager logger to logging library.

Signed-off-by: Flc゛ <[email protected]>

* refactor(log): Refactored manager logger to logging library.

Signed-off-by: Flc゛ <[email protected]>

* refactor(log): Refactored manager logger to logging library.

Signed-off-by: Flc゛ <[email protected]>

* refactor(log): Refactored manager logger to logging library.

Signed-off-by: Flc゛ <[email protected]>

---------

Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 authored Jan 22, 2024
1 parent 31c868b commit 622ddfa
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 103 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/grpc v1.60.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 h1:YJ5pD9rF8o9Qtta0Cmy9rdBwkSjrTCT6XTiUQVOtIos=
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 h1:W18sezcAYs+3tDZX4F80yctqa12jcP1PUS2gQu1zTPU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 h1:6G8oQ016D88m1xAKljMlBOOGWDZkes4kMhgGFlf8WcQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917/go.mod h1:xtjpI3tXFPP051KaWnhvxkiubL/6dJ18vLVf7q2pTOU=
google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU=
Expand Down
32 changes: 32 additions & 0 deletions log/logging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Logging

## Usage

```go
package main

import (
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/log"

"github.com/go-kratos-ecosystem/components/v2/log/logging"
)

func main() {
app := kratos.New(
kratos.Logger(newLogger()),
)

if err := app.Run(); err != nil {
log.Fatal(err)
}
}

func newLogger() *logging.Logger {
logger := logging.New(log.DefaultLogger)

logger.Register("ts", log.With(log.DefaultLogger))

return logger
}
```
48 changes: 48 additions & 0 deletions log/logging/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package logging

import (
"errors"
"fmt"

"github.com/go-kratos/kratos/v2/log"
)

var ErrNoLogger = errors.New("logging: the logger is not defined")

type Logger struct {
log.Logger
loggers map[string]log.Logger
}

func New(logger log.Logger) *Logger {
return &Logger{
Logger: logger,
loggers: make(map[string]log.Logger),
}
}

func (l *Logger) Log(level log.Level, keyvals ...interface{}) error {
if l.Logger != nil {
return l.Logger.Log(level, keyvals...)
}

return ErrNoLogger
}

func (l *Logger) Register(name string, logger log.Logger) {
l.loggers[name] = logger
}

func (l *Logger) Channel(names ...string) log.Logger {
if len(names) <= 0 {
return l.Logger
}

name := names[0]

if logger, ok := l.loggers[name]; ok {
return logger
}

panic(fmt.Errorf("logging: unknown logger %s", name))
}
31 changes: 31 additions & 0 deletions log/logging/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package logging

import (
"testing"
"time"

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

func TestManager_Channel(t *testing.T) {
logger := New(log.DefaultLogger)
logger.Register("ts", log.With(
log.DefaultLogger,
"ts", log.Timestamp(time.RFC3339),
))

logger.Log(log.LevelDebug, "test", "test") //nolint:errcheck
logger.Channel().Log(log.LevelDebug, "test", "test") //nolint:errcheck
logger.Channel("ts").Log(log.LevelDebug, "test", "test") //nolint:errcheck

assert.Panics(t, func() {
logger.Channel("unknown").Log(log.LevelDebug, "test", "test") //nolint:errcheck
})
}

func TestManager_Log(t *testing.T) {
m := New(nil)

assert.EqualError(t, m.Log(log.LevelDebug, "test", "test"), ErrNoLogger.Error())
}
63 changes: 0 additions & 63 deletions x/log/manager/manager.go

This file was deleted.

40 changes: 0 additions & 40 deletions x/log/manager/manager_test.go

This file was deleted.

0 comments on commit 622ddfa

Please sign in to comment.