-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
132 lines (110 loc) · 3.32 KB
/
message.go
File metadata and controls
132 lines (110 loc) · 3.32 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package log
import (
"fmt"
"path/filepath"
"runtime"
"strings"
"time"
)
type LogMessageMetaKV struct {
K, V string
}
type LogMessage struct {
Timestamp time.Time // timestamp
Level Level // log level
Message string // log message
Meta []LogMessageMetaKV // log metadata
trace string // stack trace (optional)
caller string // caller (optional)
loggerName string // used only in log handlers, meaningless otherwise
send func(*LogMessage)
}
// NewLogMessage
//
// Creates a new LogMessage
func NewLogMessage() *LogMessage {
return &LogMessage{
Timestamp: time.Now().UTC(),
Meta: make([]LogMessageMetaKV, 0, 1),
}
}
func (lm *LogMessage) WithSend(send func(*LogMessage)) *LogMessage { lm.send = send; return lm }
func (lm *LogMessage) Send() {
if err := lm.SendE(); err != nil {
panic(err)
}
}
func (lm *LogMessage) SendE() error {
if lm.send == nil {
return fmt.Errorf("LogMessage.SendE: no send function set")
}
lm.send(lm)
return nil
}
func (lm *LogMessage) WithMeta(key string, value any) *LogMessage {
lm.Meta = append(lm.Meta, LogMessageMetaKV{K: key, V: fmt.Sprintf("%v", value)})
return lm
}
func (lm *LogMessage) WithMetaf(key, format string, v ...any) *LogMessage {
lm.Meta = append(lm.Meta, LogMessageMetaKV{K: key, V: fmt.Sprintf(format, v...)})
return lm
}
func (lm *LogMessage) WithTraceStack() *LogMessage {
lm.trace = traceStack()
return lm
}
func (lm *LogMessage) WithCaller() *LogMessage {
lm.caller = traceCaller()
return lm
}
func (lm *LogMessage) WithLevel(level Level) *LogMessage { lm.Level = level; return lm }
func (lm *LogMessage) LevelString() string { return levelNames[lm.Level] }
func (lm *LogMessage) Msg(msg ...any) *LogMessage { lm.Message = fmt.Sprint(msg...); return lm }
func (lm *LogMessage) Msgf(format string, v ...any) *LogMessage {
lm.Message = fmt.Sprintf(format, v...)
return lm
}
func (lm *LogMessage) Debug() *LogMessage { return lm.WithLevel(DEBUG) }
func (lm *LogMessage) Info() *LogMessage { return lm.WithLevel(INFO) }
func (lm *LogMessage) Warn() *LogMessage { return lm.WithLevel(WARN) }
func (lm *LogMessage) Error() *LogMessage { return lm.WithLevel(ERROR) }
func (lm *LogMessage) Fatal() *LogMessage { return lm.WithLevel(ERROR) }
func (lm *LogMessage) String(loggerName string) string {
var metaStr string
if len(lm.Meta) > 0 {
meta := make([]string, 0, len(lm.Meta))
for _, m := range lm.Meta {
meta = append(meta, fmt.Sprintf("%s=%s", m.K, m.V))
}
metaStr = fmt.Sprintf(" {%s}", strings.Join(meta, ", "))
}
var debugStr string
if lm.trace != "" || lm.caller != "" {
debugStr = fmt.Sprintf("\n==== DEBUG ====\nCaller: %s\nTrace: %s", lm.caller, lm.trace) + "===== END =====\n\n"
}
if loggerName == "" {
loggerName = lm.loggerName
}
return strings.TrimSuffix(fmt.Sprintf("%s [%s] %s: %s%s%s",
lm.Timestamp.Format(time.RFC3339Nano),
lm.LevelString(),
loggerName,
strings.TrimSuffix(lm.Message, "\n"),
metaStr,
debugStr,
), "\n") + "\n"
}
func traceCaller() string {
pc, file, line, ok := runtime.Caller(3)
if !ok {
return "???"
}
short := filepath.Base(file)
fn := runtime.FuncForPC(pc).Name()
return fmt.Sprintf("trace: %s:%d (%s)", short, line, fn)
}
func traceStack() string {
buf := make([]byte, 4<<10)
n := runtime.Stack(buf, false)
return "stack:\n" + string(buf[:n])
}