MCPcopy
hub / github.com/TarsCloud/TarsGo / writeLine

Method writeLine

contrib/log/logger.go:100–144  ·  view source on GitHub ↗
(ctx context.Context, depth int, level rogger.LogLevel, format string, v []interface{})

Source from the content-addressed store, hash-verified

98}
99
100func (l *Logger) writeLine(ctx context.Context, depth int, level rogger.LogLevel, format string, v []interface{}) []byte {
101 buf := bytes.NewBuffer(nil)
102 if l.Writer().NeedPrefix() {
103 fmt.Fprintf(buf, "%s|", time.Now().Format("2006-01-02 15:04:05.000"))
104 if len(l.Prefix()) > 0 {
105 fmt.Fprintf(buf, "%s|", l.Prefix())
106 }
107 // trace
108 span := trace.SpanFromContext(ctx)
109 if span.IsRecording() {
110 sc := span.SpanContext()
111 fmt.Fprintf(buf, "%s|%s|", sc.TraceID(), sc.SpanID())
112 } else if t, ok := current.GetTarsTrace(ctx); ok {
113 sc := t.SpanContext()
114 fmt.Fprintf(buf, "%s|%s|", sc.TraceID(), sc.SpanID())
115 }
116
117 if rogger.CallerFlag() {
118 pc, file, line, ok := runtime.Caller(depth + callerSkip)
119 if !ok {
120 file = "???"
121 line = 0
122 } else {
123 file = filepath.Base(file)
124 }
125 fmt.Fprintf(buf, "%s:%s:%d|", file, rogger.FuncName(runtime.FuncForPC(pc)), line)
126 }
127 if rogger.IsColored() && l.IsConsoleWriter() {
128 buf.WriteString(level.ColoredString())
129 } else {
130 buf.WriteString(level.String())
131 }
132 buf.WriteByte('|')
133 }
134
135 if format == "" {
136 fmt.Fprint(buf, v...)
137 } else {
138 fmt.Fprintf(buf, format, v...)
139 }
140 if l.Writer().NeedPrefix() {
141 buf.WriteByte('\n')
142 }
143 return buf.Bytes()
144}
145
146func (l *Logger) writeJson(ctx context.Context, depth int, level rogger.LogLevel, format string, v []interface{}) []byte {
147 log := rogger.JsonLog{}

Callers 1

WritefMethod · 0.95

Calls 14

WriterMethod · 0.95
PrefixMethod · 0.95
IsConsoleWriterMethod · 0.95
WriteStringMethod · 0.95
GetTarsTraceFunction · 0.92
CallerFlagFunction · 0.92
FuncNameFunction · 0.92
IsColoredFunction · 0.92
SpanContextMethod · 0.80
TraceIDMethod · 0.80
SpanIDMethod · 0.80
ColoredStringMethod · 0.80

Tested by

no test coverage detected