-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
205 lines (167 loc) · 4.19 KB
/
handler.go
File metadata and controls
205 lines (167 loc) · 4.19 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// Copyright (C) 2021 - 2025 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/logger
//
package logger
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"path/filepath"
"runtime"
"sync"
"github.com/fogfish/logger/v3/internal/trie"
)
// JSON logger handler
func NewJSONHandler(opts ...Option) slog.Handler {
config := defaultOpts(CloudWatch...)
for _, opt := range opts {
opt(config)
}
h := slog.NewJSONHandler(config.writer,
&slog.HandlerOptions{
AddSource: config.addSource,
Level: config.level,
ReplaceAttr: config.attributes.handle,
},
)
if config.trie == nil {
return h
}
return &modTrieHandler{
Handler: h,
trie: config.trie,
}
}
//------------------------------------------------------------------------------
// The handler perform module-based logging
type modTrieHandler struct {
slog.Handler
trie *trie.Node
}
func (h *modTrieHandler) Enabled(ctx context.Context, level slog.Level) bool {
// For module-based filtering, we need to allow the handler to process the record
// and make the filtering decision in Handle() where we have access to the source path
return true
}
func (h *modTrieHandler) Handle(ctx context.Context, r slog.Record) error {
if r.PC == 0 {
return h.Handler.Handle(ctx, r)
}
fs := runtime.CallersFrames([]uintptr{r.PC})
f, _ := fs.Next()
path := filepath.Dir(f.Function)
_, n := h.trie.Lookup(path)
// If a specific module rule is found, use that rule
if len(n.Path) != 0 {
if n.Level <= r.Level {
return h.Handler.Handle(ctx, r)
}
// Module rule found but level doesn't match, don't log
return nil
}
// No specific module rule found, fall back to default handler behavior
// But we need to check if the underlying handler would accept this level
if h.Handler.Enabled(ctx, r.Level) {
return h.Handler.Handle(ctx, r)
}
return nil
}
//------------------------------------------------------------------------------
type stdioHandler struct {
w io.Writer
h slog.Handler
b *bytes.Buffer
m sync.Mutex
}
// Standard I/O handler
func NewStdioHandler(opts ...Option) slog.Handler {
config := defaultOpts(Console...)
for _, opt := range opts {
opt(config)
}
b := &bytes.Buffer{}
h := slog.NewJSONHandler(b,
&slog.HandlerOptions{
AddSource: config.addSource,
Level: config.level,
ReplaceAttr: config.attributes.handle,
},
)
if config.trie == nil {
return &stdioHandler{w: config.writer, b: b, h: h}
}
return &stdioHandler{w: config.writer, b: b,
h: &modTrieHandler{
Handler: h,
trie: config.trie,
},
}
}
func (h *stdioHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.h.Enabled(ctx, level)
}
func (h *stdioHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &stdioHandler{h: h.h.WithAttrs(attrs), b: h.b}
}
func (h *stdioHandler) WithGroup(name string) slog.Handler {
return &stdioHandler{h: h.h.WithGroup(name), b: h.b}
}
func (h *stdioHandler) Handle(ctx context.Context, r slog.Record) error {
attrs, err := h.computeAttrs(ctx, r)
if err != nil {
return err
}
// If attrs is nil, it means the message was filtered out
if attrs == nil {
return nil
}
time := attrs["time"]
level := attrs["level"]
cl := levelColorForText[r.Level]
msg := cl + r.Message + colorReset
delete(attrs, "level")
delete(attrs, "time")
delete(attrs, "msg")
if len(attrs) == 0 {
fmt.Fprintln(h.w, time, level, msg)
return nil
}
bytes, err := json.MarshalIndent(attrs, "", " ")
if err != nil {
return err
}
ca := levelColorForAttr[r.Level]
obj := ca + string(bytes) + colorReset
fmt.Fprintln(h.w, time, level, msg, obj)
return nil
}
func (h *stdioHandler) computeAttrs(
ctx context.Context,
r slog.Record,
) (map[string]any, error) {
h.m.Lock()
defer func() {
h.b.Reset()
h.m.Unlock()
}()
if err := h.h.Handle(ctx, r); err != nil {
return nil, err
}
// If buffer is empty, it means the message was filtered out by modTrieHandler
if h.b.Len() == 0 {
return nil, nil
}
var attrs map[string]any
err := json.Unmarshal(h.b.Bytes(), &attrs)
if err != nil {
return nil, err
}
return attrs, nil
}