-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpublish_view.go
More file actions
152 lines (135 loc) · 3.82 KB
/
Copy pathpublish_view.go
File metadata and controls
152 lines (135 loc) · 3.82 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
package slog
import (
"bytes"
"context"
"log/slog"
"slices"
"strings"
)
func (l *Logger) subscriptionEvent(ctx context.Context, r slog.Record) SubscriptionEvent {
published := l.publishedRecord(ctx, r)
rendered, format := l.renderSubscription(ctx, r, published)
return SubscriptionEvent{
Record: published,
Rendered: rendered,
Format: format,
}
}
func (l *Logger) publishedRecord(ctx context.Context, r slog.Record) slog.Record {
if eh := l.observerHandler(); eh != nil {
return eh.normalizedObserverRecord(ctx, r)
}
fallback := &eHandler{
opts: ext,
ctx: l.ctx,
}
return fallback.normalizedObserverRecord(ctx, r)
}
func (l *Logger) observerHandler() *eHandler {
if l == nil {
return nil
}
if eh := unwrapObserverHandler(l.text); eh != nil {
return eh
}
return unwrapObserverHandler(l.json)
}
func unwrapObserverHandler(logger *slog.Logger) *eHandler {
if logger == nil {
return nil
}
if eh, ok := logger.Handler().(*eHandler); ok {
return eh
}
return nil
}
func (l *Logger) preferredRenderedFormat() string {
textOn, jsonOn := l.outputEnabled()
if jsonOn && !textOn {
return "json"
}
if textOn {
return "text"
}
if jsonOn {
return "json"
}
return ""
}
func (l *Logger) renderSubscription(ctx context.Context, raw slog.Record, published slog.Record) (string, string) {
switch l.preferredRenderedFormat() {
case "json":
return l.renderSubscriptionJSON(ctx, raw, published), "json"
case "text":
return l.renderSubscriptionText(ctx, raw, published), "text"
default:
return "", ""
}
}
func (l *Logger) renderSubscriptionText(ctx context.Context, raw slog.Record, published slog.Record) string {
if l != nil && l.text != nil {
return l.renderWithHandlerChain(ctx, raw, l.text.Handler(), func(buf *bytes.Buffer) slog.Handler {
return NewConsoleHandler(buf, l.noColor, l.subscriptionHandlerOptions())
})
}
return l.renderPublishedText(published)
}
func (l *Logger) renderSubscriptionJSON(ctx context.Context, raw slog.Record, published slog.Record) string {
if l != nil && l.json != nil {
return l.renderWithHandlerChain(ctx, raw, l.json.Handler(), func(buf *bytes.Buffer) slog.Handler {
return NewJSONHandler(buf, l.subscriptionHandlerOptions())
})
}
return l.renderPublishedJSON(published)
}
func (l *Logger) renderPublishedText(record slog.Record) string {
var buf bytes.Buffer
handler := NewConsoleHandler(&buf, l.noColor, l.subscriptionHandlerOptions())
if err := handler.Handle(context.Background(), record); err != nil {
return ""
}
return strings.TrimSuffix(buf.String(), "\n")
}
func (l *Logger) renderWithHandlerChain(ctx context.Context, raw slog.Record, original slog.Handler, baseFactory func(*bytes.Buffer) slog.Handler) string {
var buf bytes.Buffer
renderer := cloneRenderChain(original, baseFactory(&buf))
if renderer == nil {
return ""
}
if err := renderer.Handle(ctx, raw); err != nil {
return ""
}
return strings.TrimSuffix(buf.String(), "\n")
}
func cloneRenderChain(original slog.Handler, leaf slog.Handler) slog.Handler {
if original == nil {
return leaf
}
eh, ok := original.(*eHandler)
if !ok {
return leaf
}
next := cloneRenderChain(eh.handler, leaf)
return &eHandler{
handler: next,
opts: eh.opts,
prefixes: slices.Clone(eh.prefixes),
groups: slices.Clone(eh.groups),
observerOps: cloneObserverOperations(eh.observerOps),
ctx: eh.ctx,
}
}
func (l *Logger) renderPublishedJSON(record slog.Record) string {
var buf bytes.Buffer
handler := NewJSONHandler(&buf, l.subscriptionHandlerOptions())
if err := handler.Handle(context.Background(), record); err != nil {
return ""
}
return strings.TrimSuffix(buf.String(), "\n")
}
func (l *Logger) subscriptionHandlerOptions() *slog.HandlerOptions {
return &slog.HandlerOptions{
AddSource: l.renderConfig.addSource,
ReplaceAttr: l.renderConfig.replaceAttr,
}
}