add logger wrapper (#715)

* add logger wrapper
pull/717/head
Tony Chen 4 years ago committed by GitHub
parent 283313c2a1
commit 18d43d6006
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      log/log.go
  2. 2
      log/log_test.go
  3. 6
      log/std.go
  4. 2
      log/std_test.go
  5. 14
      log/wrapper.go
  6. 14
      log/wrapper_test.go

@ -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")

@ -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)
}

@ -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…
Cancel
Save