MCPcopy
hub / github.com/pterm/pterm / Handle

Method Handle

slog_handler.go:30–72  ·  view source on GitHub ↗

Handle handles the given record.

(ctx context.Context, record slog.Record)

Source from the content-addressed store, hash-verified

28
29// Handle handles the given record.
30func (s *SlogHandler) Handle(ctx context.Context, record slog.Record) error {
31 level := record.Level
32 message := record.Message
33
34 // Convert slog Attrs to a map.
35 keyValsMap := make(map[string]any)
36
37 record.Attrs(func(attr slog.Attr) bool {
38 keyValsMap[attr.Key] = attr.Value
39 return true
40 })
41
42 for _, attr := range s.attrs {
43 keyValsMap[attr.Key] = attr.Value
44 }
45
46 args := s.logger.ArgsFromMap(keyValsMap)
47
48 // Wrapping args inside another slice to match [][]LoggerArgument
49 argsWrapped := [][]LoggerArgument{args}
50
51 logger := s.logger
52
53 // Must be done here, see https://github.com/pterm/pterm/issues/608#issuecomment-1876001650
54 if logger.CallerOffset == 0 {
55 logger = logger.WithCallerOffset(3)
56 }
57
58 switch level {
59 case slog.LevelDebug:
60 logger.Debug(message, argsWrapped...)
61 case slog.LevelInfo:
62 logger.Info(message, argsWrapped...)
63 case slog.LevelWarn:
64 logger.Warn(message, argsWrapped...)
65 case slog.LevelError:
66 logger.Error(message, argsWrapped...)
67 default:
68 logger.Print(message, argsWrapped...)
69 }
70
71 return nil
72}
73
74// WithAttrs returns a new handler with the given attributes.
75func (s *SlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {

Callers

nothing calls this directly

Calls 7

ArgsFromMapMethod · 0.80
WithCallerOffsetMethod · 0.80
DebugMethod · 0.80
WarnMethod · 0.80
ErrorMethod · 0.80
PrintMethod · 0.65
InfoMethod · 0.45

Tested by

no test coverage detected