feat:add context for log (#2041)

* add context
pull/2043/head
longxboy 3 years ago committed by GitHub
parent 70317d05a0
commit 944d480f78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      log/global.go
  2. 10
      log/global_test.go

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

@ -2,6 +2,7 @@ package log
import (
"bytes"
"context"
"fmt"
"os"
"strings"
@ -83,3 +84,12 @@ func TestGlobalLogUpdate(t *testing.T) {
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