mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-10 00:29:01 +02:00
parent
283313c2a1
commit
18d43d6006
@ -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.
|
||||
|
@ -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")
|
||||
|
@ -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)
|
||||
|
@ -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
14
log/wrapper.go
Normal 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
14
log/wrapper_test.go
Normal 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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user