-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
149 lines (134 loc) · 3.57 KB
/
handler.go
File metadata and controls
149 lines (134 loc) · 3.57 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package logi
import (
"fmt"
"io"
"log/slog"
"path"
"runtime"
"strings"
"time"
)
func ParseLevel(level string) *slog.LevelVar {
slogLevel := new(slog.LevelVar)
switch strings.ToLower(level) {
case "debug", "dbg":
slogLevel.Set(slog.LevelDebug)
case "info", "inf":
slogLevel.Set(slog.LevelInfo)
case "warning", "warn":
slogLevel.Set(slog.LevelWarn)
case "error", "err":
slogLevel.Set(slog.LevelError)
default:
slogLevel.Set(slog.LevelInfo)
}
return slogLevel
}
type ReplaceAttrFunc func(groups []string, a slog.Attr) slog.Attr
type VerbosityHandlers func(verbosity int)
type HandlerOptions struct {
Format string // console, json, text
Color bool
Level string
Attrs map[string]any
Writers []io.Writer
Verbosity int
VerbosityHandlers []VerbosityHandlers
CallerSkip int
ReplaceAttrs []ReplaceAttrFunc
}
func NewHandler(opt HandlerOptions) slog.Handler {
// level
slogLevel := ParseLevel(opt.Level)
// replace
replaceChain := NewReplaceAttrChain()
if len(opt.ReplaceAttrs) > 0 {
for _, replaceAttr := range opt.ReplaceAttrs {
replaceChain.Append(replaceAttr)
}
}
// time
replaceChain.Append(func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.String(a.Key, a.Value.Time().Format(time.RFC3339))
}
return a
})
// verbosity
var addSource bool
switch opt.Verbosity {
case 0:
addSource = false
case 1:
addSource = true
replaceChain.Append(func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.SourceKey {
pc, file, line, _ := runtime.Caller(opt.CallerSkip)
fileName := path.Base(file)
funcName := path.Base(runtime.FuncForPC(pc).Name())
funcNames := strings.Split(funcName, ".")
funcName = funcNames[len(funcNames)-1]
return slog.String(slog.SourceKey, fmt.Sprintf("%s:%s:%d", fileName, funcName, line))
}
return a
})
case 2:
addSource = true
replaceChain.Append(func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.SourceKey {
pc, file, line, _ := runtime.Caller(opt.CallerSkip)
fileName := path.Base(file)
funcName := path.Base(runtime.FuncForPC(pc).Name())
return slog.String(slog.SourceKey, fmt.Sprintf("%s:%s:%d", fileName, funcName, line))
}
return a
})
case 3:
addSource = true
replaceChain.Append(func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.SourceKey {
pc, file, line, _ := runtime.Caller(opt.CallerSkip)
fileName := path.Base(file)
funcName := path.Base(runtime.FuncForPC(pc).Name())
return slog.Any(slog.SourceKey, slog.Source{
Function: funcName,
File: fileName,
Line: line,
})
}
return a
})
case 4:
addSource = true
}
for _, handle := range opt.VerbosityHandlers {
handle(opt.Verbosity)
}
// options
handlerOpts := &slog.HandlerOptions{
AddSource: addSource,
Level: slogLevel,
ReplaceAttr: replaceChain.ReplaceAttr,
}
// writer
groupWriter := io.MultiWriter(opt.Writers...)
// format
var handler slog.Handler
switch strings.ToLower(opt.Format) {
case "text":
handler = slog.NewTextHandler(groupWriter, handlerOpts)
case "console":
handler = NewConsoleHandler(groupWriter, handlerOpts, opt.Color)
case "json":
handler = NewJSONHandler(groupWriter, handlerOpts, opt.Color)
default:
handler = slog.NewTextHandler(groupWriter, handlerOpts)
}
// attrs
slogAttrs := make([]slog.Attr, 0, len(opt.Attrs))
for key, value := range opt.Attrs {
slogAttrs = append(slogAttrs, slog.Any(key, value))
}
handler = handler.WithAttrs(slogAttrs)
return handler
}