Skip to content

Commit

Permalink
reset context.Context to interface{} for tracing logger
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyibing committed Feb 23, 2021
1 parent f3f8505 commit 79f9619
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 43 deletions.
11 changes: 5 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package log

import (
"context"
"fmt"
"os"

Expand Down Expand Up @@ -35,7 +34,7 @@ func (o *client) Debugf(text string, args ...interface{}) {
}

// 添加Debug日志, 支持格式化和请求链.
func (o *client) Debugfc(ctx context.Context, text string, args ...interface{}) {
func (o *client) Debugfc(ctx interface{}, text string, args ...interface{}) {
if Config.DebugOn() {
o.log(ctx, interfaces.LevelDebug, text, args...)
}
Expand All @@ -56,7 +55,7 @@ func (o *client) Infof(text string, args ...interface{}) {
}

// 添加Info日志, 支持格式化和请求链.
func (o *client) Infofc(ctx context.Context, text string, args ...interface{}) {
func (o *client) Infofc(ctx interface{}, text string, args ...interface{}) {
if Config.InfoOn() {
o.log(ctx, interfaces.LevelInfo, text, args...)
}
Expand All @@ -77,7 +76,7 @@ func (o *client) Warnf(text string, args ...interface{}) {
}

// 添加Warn日志, 支持格式化和请求链.
func (o *client) Warnfc(ctx context.Context, text string, args ...interface{}) {
func (o *client) Warnfc(ctx interface{}, text string, args ...interface{}) {
if Config.WarnOn() {
o.log(ctx, interfaces.LevelWarn, text, args...)
}
Expand All @@ -98,14 +97,14 @@ func (o *client) Errorf(text string, args ...interface{}) {
}

// 添加Error日志, 支持格式化和请求链.
func (o *client) Errorfc(ctx context.Context, text string, args ...interface{}) {
func (o *client) Errorfc(ctx interface{}, text string, args ...interface{}) {
if Config.ErrorOn() {
o.log(ctx, interfaces.LevelError, text, args...)
}
}

// 日志处理逻辑.
func (o *client) log(ctx context.Context, level interfaces.Level, text string, args ...interface{}) {
func (o *client) log(ctx interface{}, level interfaces.Level, text string, args ...interface{}) {
if handler := Config.GetHandler(); handler != nil {
handler(NewLine(ctx, level, text, args))
} else {
Expand Down
16 changes: 6 additions & 10 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ package log

import (
"context"
"net/http"

"github.com/kataras/iris/v12"

"github.com/fuyibing/log/v2/interfaces"
)
Expand All @@ -14,16 +15,11 @@ import (
// 在请求的入口进行绑定, 请求过程即可复用. 整个业务过程中使用
// 绑定后的Context, 可以保障同一个请求下的日志含相同的Span、
// Trace待标识.
func BindRequest(req *http.Request) {
// Bound and reuse.
if ctx := req.Context().Value(interfaces.OpenTracingKey); ctx != nil {
if t, ok := ctx.(*tracing); ok {
t.UseRequest(req)
return
}
func IrisBind(ctx iris.Context) {
if ctx == nil {
return
}
// New bind.
req.WithContext(context.WithValue(context.TODO(), interfaces.OpenTracingKey, NewTracing().UseRequest(req)))
ctx.Values().Set(interfaces.OpenTracingKey, NewTracing().UseRequest(ctx.Request()))
}

// 创建上下文.
Expand Down
12 changes: 4 additions & 8 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

package log

import (
"context"
)

// 添加Debug日志.
func Debug(text string) {
if Config.DebugOn() {
Expand All @@ -22,7 +18,7 @@ func Debugf(text string, args ...interface{}) {
}

// 添加Debug日志, 支持格式化和请求链.
func Debugfc(ctx context.Context, text string, args ...interface{}) {
func Debugfc(ctx interface{}, text string, args ...interface{}) {
if Config.DebugOn() {
Client.Debugfc(ctx, text, args...)
}
Expand All @@ -43,7 +39,7 @@ func Infof(text string, args ...interface{}) {
}

// 添加Info日志, 支持格式化和请求链.
func Infofc(ctx context.Context, text string, args ...interface{}) {
func Infofc(ctx interface{}, text string, args ...interface{}) {
if Config.InfoOn() {
Client.Infofc(ctx, text, args...)
}
Expand All @@ -64,7 +60,7 @@ func Warnf(text string, args ...interface{}) {
}

// 添加Warn日志, 支持格式化和请求链.
func Warnfc(ctx context.Context, text string, args ...interface{}) {
func Warnfc(ctx interface{}, text string, args ...interface{}) {
if Config.WarnOn() {
Client.Warnfc(ctx, text, args...)
}
Expand All @@ -85,7 +81,7 @@ func Errorf(text string, args ...interface{}) {
}

// 添加Error日志, 支持格式化和请求链.
func Errorfc(ctx context.Context, text string, args ...interface{}) {
func Errorfc(ctx interface{}, text string, args ...interface{}) {
if Config.ErrorOn() {
Client.Errorfc(ctx, text, args...)
}
Expand Down
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@ module github.com/fuyibing/log/v2
go 1.13

require (
github.com/ajg/form v1.5.1 // indirect
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
github.com/gomodule/redigo v1.8.4
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/uuid v1.2.0
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/kataras/iris/v12 v12.1.8
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/moul/http2curl v1.0.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/valyala/fasthttp v1.21.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yudai/pp v2.0.1+incompatible // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
xorm.io/xorm v1.0.7
)
Loading

0 comments on commit 79f9619

Please sign in to comment.