feat:add context for log (#2041)

* add context
status-code-override
longxboy 3 years ago committed by chenzhihui
parent ded51d352c
commit 57a935c8d1
  1. 5
      log/global.go
  2. 10
      log/global_test.go

@ -1,6 +1,7 @@
package log package log
import ( import (
"context"
"sync" "sync"
) )
@ -46,6 +47,10 @@ func Log(level Level, keyvals ...interface{}) {
global.helper.Log(level, keyvals...) global.helper.Log(level, keyvals...)
} }
func Context(ctx context.Context) *Helper {
return global.helper.WithContext(ctx)
}
// Debug logs a message at debug level. // Debug logs a message at debug level.
func Debug(a ...interface{}) { func Debug(a ...interface{}) {
global.helper.Debug(a...) global.helper.Debug(a...)

@ -2,6 +2,7 @@ package log
import ( import (
"bytes" "bytes"
"context"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -83,3 +84,12 @@ func TestGlobalLogUpdate(t *testing.T) {
t.Errorf("Expected: %s, got: %s", expected, buffer.String()) t.Errorf("Expected: %s, got: %s", expected, buffer.String())
} }
} }
func TestGolbalContext(t *testing.T) {
buffer := &bytes.Buffer{}
SetLogger(NewStdLogger(buffer))
Context(context.Background()).Infof("111")
if buffer.String() != "INFO msg=111\n" {
t.Errorf("Expected:%s, got:%s", "INFO msg=111", buffer.String())
}
}

Loading…
Cancel
Save