1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-10 00:29:01 +02:00

add logger wrapper (#715)

* add logger wrapper
This commit is contained in:
Tony Chen 2021-02-22 17:50:43 +08:00 committed by GitHub
parent 283313c2a1
commit 18d43d6006
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 6 deletions

View File

@ -1,8 +1,10 @@
package log
import "os"
var (
// DefaultLogger is default logger.
DefaultLogger Logger = NewStdLogger()
DefaultLogger Logger = NewStdLogger(os.Stderr)
)
// Logger is a logger interface.

View File

@ -5,7 +5,7 @@ import (
)
func TestLogger(t *testing.T) {
logger := NewStdLogger()
logger := DefaultLogger
Debug(logger).Print("log", "test debug")
Info(logger).Print("log", "test info")
Warn(logger).Print("log", "test warn")

View File

@ -3,8 +3,8 @@ package log
import (
"bytes"
"fmt"
"io"
"log"
"os"
"sync"
)
@ -16,9 +16,9 @@ type stdLogger struct {
}
// NewStdLogger new a std logger with options.
func NewStdLogger() Logger {
func NewStdLogger(w io.Writer) Logger {
return &stdLogger{
log: log.New(os.Stderr, "", log.LstdFlags),
log: log.New(w, "", log.LstdFlags),
pool: &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)

View File

@ -3,7 +3,7 @@ package log
import "testing"
func TestFmtLogger(t *testing.T) {
logger := NewStdLogger()
logger := DefaultLogger
Debug(logger).Print("log", "test debug")
Info(logger).Print("log", "test info")

14
log/wrapper.go Normal file
View File

@ -0,0 +1,14 @@
package log
type wrapper []Logger
func (w wrapper) Print(pairs ...interface{}) {
for _, p := range w {
p.Print(pairs...)
}
}
// Wrap wraps multi logger.
func Wrap(l ...Logger) Logger {
return wrapper(l)
}

14
log/wrapper_test.go Normal file
View File

@ -0,0 +1,14 @@
package log
import (
"os"
"testing"
)
func TestWrapper(t *testing.T) {
out := NewStdLogger(os.Stdout)
err := NewStdLogger(os.Stderr)
l := Wrap(out, err)
l.Print("message", "test")
}