MCPcopy
hub / github.com/uber-go/zap / Handle

Method Handle

exp/zapslog/handler.go:138–188  ·  view source on GitHub ↗

Handle handles the Record.

(ctx context.Context, record slog.Record)

Source from the content-addressed store, hash-verified

136
137// Handle handles the Record.
138func (h *Handler) Handle(ctx context.Context, record slog.Record) error {
139 ent := zapcore.Entry{
140 Level: convertSlogLevel(record.Level),
141 Time: record.Time,
142 Message: record.Message,
143 LoggerName: h.name,
144 }
145 ce := h.core.Check(ent, nil)
146 if ce == nil {
147 return nil
148 }
149
150 if h.addCaller && record.PC != 0 {
151 frame, _ := runtime.CallersFrames([]uintptr{record.PC}).Next()
152 if frame.PC != 0 {
153 ce.Caller = zapcore.EntryCaller{
154 Defined: true,
155 PC: frame.PC,
156 File: frame.File,
157 Line: frame.Line,
158 Function: frame.Function,
159 }
160 }
161 }
162
163 if record.Level >= h.addStackAt {
164 // Skipping 3:
165 // zapslog/handler log/slog.(*Logger).log
166 // slog/logger log/slog.(*Logger).log
167 // slog/logger log/slog.(*Logger).<level>
168 ce.Stack = stacktrace.Take(3 + h.callerSkip)
169 }
170
171 fields := make([]zapcore.Field, 0, record.NumAttrs()+len(h.groups))
172
173 var addedNamespace bool
174 record.Attrs(func(attr slog.Attr) bool {
175 f := convertAttrToField(attr)
176 if !addedNamespace && len(h.groups) > 0 && f != zap.Skip() {
177 // Namespaces are added only if at least one field is present
178 // to avoid creating empty groups.
179 fields = h.appendGroups(fields)
180 addedNamespace = true
181 }
182 fields = append(fields, f)
183 return true
184 })
185
186 ce.Write(fields...)
187 return nil
188}
189
190func (h *Handler) appendGroups(fields []zapcore.Field) []zapcore.Field {
191 for _, g := range h.groups {

Callers

nothing calls this directly

Calls 8

appendGroupsMethod · 0.95
TakeFunction · 0.92
SkipFunction · 0.92
convertSlogLevelFunction · 0.85
convertAttrToFieldFunction · 0.85
NextMethod · 0.80
CheckMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected