Skip to content

Commit c6e26a6

Browse files
authored
Merge pull request #5 from fuyibing/2.x
支持链解析
2 parents 099326f + 56ed540 commit c6e26a6

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed

line.go

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
package log
55

66
import (
7-
"context"
87
"fmt"
98
"regexp"
109
"strconv"
1110
"time"
1211

13-
"github.com/kataras/iris/v12"
14-
1512
"github.com/fuyibing/log/v2/interfaces"
1613
)
1714

@@ -25,6 +22,7 @@ var (
2522
regexpLineDuration = regexp.MustCompile(`\[d=(\d+\.?\d*)\]`)
2623
)
2724

25+
// 日志行结构体.
2826
type Line struct {
2927
args []interface{}
3028
duration float64
@@ -38,18 +36,26 @@ type Line struct {
3836
tracing interfaces.TraceInterface
3937
}
4038

39+
// 创建日志行实例.
4140
func NewLine(ctx interface{}, level interfaces.Level, text string, args []interface{}) interfaces.LineInterface {
41+
// 行实例.
4242
o := &Line{
4343
time: time.Now(),
4444
text: text, args: args, level: level,
4545
pid: Config.GetPid(),
4646
serviceName: Config.AppName(), serviceAddr: Config.AppAddr(),
4747
}
48+
// 执行时长.
4849
o.parseDuration()
49-
o.parseTracing(ctx)
50+
// 请求链.
51+
if tracer := ParseTracing(ctx); tracer != nil {
52+
o.tracing = tracer
53+
o.offset, _ = o.tracing.IncrOffset()
54+
}
5055
return o
5156
}
5257

58+
// 返回带颜色Level文本.
5359
func (o *Line) ColorLevel() string {
5460
if c, ok := colors[o.level]; ok {
5561
return fmt.Sprintf("%c[%d;%d;%dm%5s%c[0m",
@@ -62,30 +68,36 @@ func (o *Line) ColorLevel() string {
6268
return o.Level()
6369
}
6470

71+
// 日志正文.
6572
func (o *Line) Content() string {
6673
if o.args != nil && len(o.args) > 0 {
6774
return fmt.Sprintf(o.text, o.args...)
6875
}
6976
return o.text
7077
}
7178

79+
// 执行时长.
7280
func (o *Line) Duration() float64 {
7381
return o.duration
7482
}
7583

84+
// 日志级别.
7685
func (o *Line) Level() string { return Config.GetLevel(o.level) }
7786

87+
// 上级Span.
7888
func (o *Line) ParentSpanId() string {
7989
if o.tracing != nil {
8090
return o.tracing.GetParentSpanId()
8191
}
8292
return ""
8393
}
8494

95+
// 进程ID.
8596
func (o *Line) Pid() int {
8697
return o.pid
8798
}
8899

100+
// 请求信息.
89101
func (o *Line) RequestInfo() (method string, url string) {
90102
if o.tracing != nil {
91103
method, url = o.tracing.RequestInfo()
@@ -138,35 +150,3 @@ func (o *Line) parseDuration() {
138150
}
139151
}
140152
}
141-
142-
// Parse tracing from Context.
143-
// ctx accept mixed struct instance, allow:
144-
// iris.Context, context.Context, Tracing
145-
func (o *Line) parseTracing(ctx interface{}) {
146-
// nil.
147-
if ctx == nil {
148-
return
149-
}
150-
// Use iris.Context.
151-
if ir, ok := ctx.(iris.Context); ok {
152-
if x := ir.Values().Get(interfaces.OpenTracingKey); x != nil {
153-
o.tracing = x.(interfaces.TraceInterface)
154-
o.offset, _ = o.tracing.IncrOffset()
155-
}
156-
return
157-
}
158-
// Use context.Context.
159-
if cc, ok := ctx.(context.Context); ok {
160-
if x := cc.Value(interfaces.OpenTracingKey); x != nil {
161-
o.tracing = x.(interfaces.TraceInterface)
162-
o.offset, _ = o.tracing.IncrOffset()
163-
}
164-
return
165-
}
166-
// Use TraceInterface
167-
if ti, ok := ctx.(interfaces.TraceInterface); ok {
168-
o.tracing = ti.(interfaces.TraceInterface)
169-
o.offset, _ = o.tracing.IncrOffset()
170-
return
171-
}
172-
}

tracing.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package log
55

66
import (
7+
"context"
78
"fmt"
89
"math/rand"
910
"net/http"
@@ -12,6 +13,7 @@ import (
1213
"time"
1314

1415
"github.com/google/uuid"
16+
"github.com/kataras/iris/v12"
1517

1618
"github.com/fuyibing/log/v2/interfaces"
1719
)
@@ -94,3 +96,41 @@ func (o *tracing) parseHeader(header http.Header) interfaces.TraceInterface {
9496
// with header.
9597
return o
9698
}
99+
100+
// 解析Tracing.
101+
func ParseTracing(ctx interface{}) interfaces.TraceInterface {
102+
// nil.
103+
if ctx == nil {
104+
return nil
105+
}
106+
// Use iris.Context.
107+
if ir, ok := ctx.(iris.Context); ok {
108+
if x := ir.Values().Get(interfaces.OpenTracingKey); x != nil {
109+
return x.(interfaces.TraceInterface)
110+
}
111+
}
112+
// Use context.Context.
113+
if cc, ok := ctx.(context.Context); ok {
114+
if x := cc.Value(interfaces.OpenTracingKey); x != nil {
115+
return x.(interfaces.TraceInterface)
116+
}
117+
}
118+
// Use TraceInterface
119+
if ti, ok := ctx.(interfaces.TraceInterface); ok {
120+
return ti.(interfaces.TraceInterface)
121+
}
122+
// Undefined.
123+
return nil
124+
}
125+
126+
func ParseTracingToRequest(ctx interface{}, req *http.Request) {
127+
trace := ParseTracing(ctx)
128+
if trace == nil {
129+
return
130+
}
131+
tracer := trace.(*tracing)
132+
traceId, spanId, spanVersion := Config.GetTrace()
133+
req.Header.Set(traceId, tracer.traceId)
134+
req.Header.Set(spanId, tracer.spanId)
135+
req.Header.Set(spanVersion, tracer.GenVersion(tracer.offset-1))
136+
}

0 commit comments

Comments
 (0)