-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(log): Refactored manager logger to logging library. (#72)
* 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
Showing
7 changed files
with
116 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.