44package log
55
66import (
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
2522 regexpLineDuration = regexp .MustCompile (`\[d=(\d+\.?\d*)\]` )
2623)
2724
25+ // 日志行结构体.
2826type Line struct {
2927 args []interface {}
3028 duration float64
@@ -38,18 +36,26 @@ type Line struct {
3836 tracing interfaces.TraceInterface
3937}
4038
39+ // 创建日志行实例.
4140func 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文本.
5359func (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+ // 日志正文.
6572func (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+ // 执行时长.
7280func (o * Line ) Duration () float64 {
7381 return o .duration
7482}
7583
84+ // 日志级别.
7685func (o * Line ) Level () string { return Config .GetLevel (o .level ) }
7786
87+ // 上级Span.
7888func (o * Line ) ParentSpanId () string {
7989 if o .tracing != nil {
8090 return o .tracing .GetParentSpanId ()
8191 }
8292 return ""
8393}
8494
95+ // 进程ID.
8596func (o * Line ) Pid () int {
8697 return o .pid
8798}
8899
100+ // 请求信息.
89101func (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- }
0 commit comments