-
Notifications
You must be signed in to change notification settings - Fork 0
/
logrusAdapter.go
158 lines (133 loc) · 3.38 KB
/
logrusAdapter.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package PrettyLogger
// refs:
// https://josephwoodward.co.uk/2022/11/slog-structured-logging-proposal
// https://thedevelopercafe.com/articles/logging-in-go-with-slog-a7bb489755c2
import (
"context"
"fmt"
"log/slog"
"sort"
"strings"
"sync"
"sync/atomic"
"github.com/sirupsen/logrus"
)
type LogrusHandler struct {
logger *logrus.Logger
}
func NewLogrusHandler(logger *logrus.Logger) *LogrusHandler {
return &LogrusHandler{
logger: logger,
}
}
func ConvertLogLevel(level string) logrus.Level {
var l logrus.Level
switch strings.ToLower(level) {
case "error":
l = logrus.ErrorLevel
case "warn":
l = logrus.WarnLevel
case "info":
l = logrus.InfoLevel
case "debug":
l = logrus.DebugLevel
default:
l = logrus.InfoLevel
}
return l
}
//func (h *LogrusHandler) Enabled(_ slog.Level) bool {
// // support all logging levels
// return true
//}
func (h *LogrusHandler) Enabled(_ context.Context, _ slog.Level) bool {
// support all logging levels
return true
}
var functionNames sync.Map
var colorFuncs = []func(string) string{
cf.ColorBrightBlue,
cf.ColorPurple,
cf.ColorPink,
cf.ColorBrightCyan,
cf.ColorBrightYellow,
cf.ColorBrightGreen,
}
var index int64
func getFuncNameWithColor(name string) string {
if formattedName, l := functionNames.Load(name); l {
return formattedName.(string)
}
formattedName, _ := functionNames.LoadOrStore(name, colorFuncs[int(atomic.AddInt64(&index, 1)-1)%len(colorFuncs)](name))
return formattedName.(string)
}
func (h *LogrusHandler) Handle(ctx context.Context, rec slog.Record) error {
fields := make(map[string]interface{}, rec.NumAttrs())
rec.Attrs(func(a slog.Attr) bool {
fields[a.Key] = a.Value.Any()
return true // continue iteration
})
entry := h.logger.WithFields(fields)
printMsg := ""
if rec.Level != slog.LevelError {
fnName := cf.Italic(getFuncNameWithColor(GetFuncNameWithSkip(4)))
loc := getLocation(4)
if len(fields) > 0 {
//str := ""
// Step 1: Extract the keys from the map
keys := make([]string, 0, len(fields))
for key, _ := range fields {
keys = append(keys, key)
}
// Step 2: Sort the keys
sort.Strings(keys)
vars := make([]string, len(keys))
for i, k := range keys {
paramStr := interfaceToString(fields[k])
vars[i] = fmt.Sprintf("%s=%s", cf.Italic(cf.ColorCyan(k)), cf.Bold(paramStr))
//str = str + fmt.Sprintf("%s=%v, ", k, fields[k])
}
str := strings.Join(vars, ", ")
printMsg = fmt.Sprintf("%s (%s) → %s | %s", loc, fnName, rec.Message, str)
} else {
printMsg = fmt.Sprintf("%s (%s) → %s", loc, fnName, rec.Message)
}
}
switch rec.Level {
case slog.LevelDebug:
entry.Debug(printMsg)
case slog.LevelInfo:
entry.Info(printMsg)
case slog.LevelWarn:
entry.Warn(printMsg)
case slog.LevelError:
//stack := ""
b := false
for _, v := range fields {
//if k == "err" {
if e, ok := v.(error); ok {
ee := wrapError(e, rec.Message)
str := fmt.Sprintf("%+v", ee)
ew := parseWrappedError(str)
ew = strings.TrimSpace(ew)
i := strings.Index(ew, "\n")
fmt.Println()
entry.Error(cf.ColorRed(ew[:i]) + "\n" + ew[i+1:] + "\n")
b = true
}
//}
}
if !b {
entry.Error(rec.Message)
}
}
return nil
}
func (h *LogrusHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
// not implemented for brevity
return h
}
func (h *LogrusHandler) WithGroup(name string) slog.Handler {
// not implemented for brevity
return h
}