-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction_logging.go
86 lines (77 loc) · 2.11 KB
/
transaction_logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package msmtpd
import (
"fmt"
"log"
"go.opentelemetry.io/otel/codes"
)
func (t *Transaction) logEvent(level LoggerLevel, format string, args ...any) {
var backend Logger
if t.Logger != nil {
backend = t.Logger
} else {
if t.server != nil {
if t.server.Logger != nil {
backend = t.server.Logger
}
} else {
backend = &DefaultLogger{
Logger: log.Default(),
Level: DebugLevel,
}
}
}
if t.Span != nil {
t.Span.AddEvent(level.String() + " " + fmt.Sprintf(format, args...))
}
switch level {
case TraceLevel:
backend.Tracef(t, format, args...)
break
case DebugLevel:
backend.Debugf(t, format, args...)
break
case InfoLevel:
backend.Infof(t, format, args...)
break
case WarnLevel:
backend.Warnf(t, format, args...)
break
case ErrorLevel:
backend.Errorf(t, format, args...)
break
case FatalLevel:
backend.Fatalf(t, format, args...)
break
default:
backend.Infof(t, format, args...)
}
}
// LogTrace is used to send trace level message to server logger
func (t *Transaction) LogTrace(format string, args ...any) {
t.logEvent(TraceLevel, format, args...)
}
// LogDebug is used to send debug level message to server logger
func (t *Transaction) LogDebug(format string, args ...any) {
t.logEvent(DebugLevel, format, args...)
}
// LogInfo is used to send info level message to server logger
func (t *Transaction) LogInfo(format string, args ...any) {
t.logEvent(InfoLevel, format, args...)
}
// LogWarn is used to send warning level message to server logger
func (t *Transaction) LogWarn(format string, args ...any) {
t.logEvent(WarnLevel, format, args...)
}
// LogError is used to send error level message to server logger. This function marks
// OpenTelemetry Transaction's span as erroneous one
func (t *Transaction) LogError(err error, desc string) {
if t.Span != nil {
t.Span.RecordError(err)
t.Span.SetStatus(codes.Error, desc) // marks span as having errors
}
t.logEvent(ErrorLevel, "%s : %s", err, desc)
}
// LogFatal is used to send error level message to server logger
func (t *Transaction) LogFatal(err error, desc string) {
t.logEvent(FatalLevel, "%s %s", err, desc)
}