mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-24 03:46:37 +02:00
parent
283313c2a1
commit
18d43d6006
@ -1,8 +1,10 @@
|
|||||||
package log
|
package log
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// DefaultLogger is default logger.
|
// DefaultLogger is default logger.
|
||||||
DefaultLogger Logger = NewStdLogger()
|
DefaultLogger Logger = NewStdLogger(os.Stderr)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Logger is a logger interface.
|
// Logger is a logger interface.
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestLogger(t *testing.T) {
|
func TestLogger(t *testing.T) {
|
||||||
logger := NewStdLogger()
|
logger := DefaultLogger
|
||||||
Debug(logger).Print("log", "test debug")
|
Debug(logger).Print("log", "test debug")
|
||||||
Info(logger).Print("log", "test info")
|
Info(logger).Print("log", "test info")
|
||||||
Warn(logger).Print("log", "test warn")
|
Warn(logger).Print("log", "test warn")
|
||||||
|
@ -3,8 +3,8 @@ package log
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,9 +16,9 @@ type stdLogger struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewStdLogger new a std logger with options.
|
// NewStdLogger new a std logger with options.
|
||||||
func NewStdLogger() Logger {
|
func NewStdLogger(w io.Writer) Logger {
|
||||||
return &stdLogger{
|
return &stdLogger{
|
||||||
log: log.New(os.Stderr, "", log.LstdFlags),
|
log: log.New(w, "", log.LstdFlags),
|
||||||
pool: &sync.Pool{
|
pool: &sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return new(bytes.Buffer)
|
return new(bytes.Buffer)
|
||||||
|
@ -3,7 +3,7 @@ package log
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestFmtLogger(t *testing.T) {
|
func TestFmtLogger(t *testing.T) {
|
||||||
logger := NewStdLogger()
|
logger := DefaultLogger
|
||||||
|
|
||||||
Debug(logger).Print("log", "test debug")
|
Debug(logger).Print("log", "test debug")
|
||||||
Info(logger).Print("log", "test info")
|
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…
x
Reference in New Issue
Block a user