NewLoggingHandler creates a [LoggingHandler] that logs to the given [ServerSession] using a [slog.JSONHandler].
(ss *ServerSession, opts *LoggingHandlerOptions)
| 100 | // NewLoggingHandler creates a [LoggingHandler] that logs to the given [ServerSession] using a |
| 101 | // [slog.JSONHandler]. |
| 102 | func NewLoggingHandler(ss *ServerSession, opts *LoggingHandlerOptions) *LoggingHandler { |
| 103 | var buf bytes.Buffer |
| 104 | jsonHandler := slog.NewJSONHandler(&buf, &slog.HandlerOptions{ |
| 105 | ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr { |
| 106 | // Remove level: it appears in LoggingMessageParams. |
| 107 | if a.Key == slog.LevelKey { |
| 108 | return slog.Attr{} |
| 109 | } |
| 110 | return a |
| 111 | }, |
| 112 | }) |
| 113 | lh := &LoggingHandler{ |
| 114 | ss: ss, |
| 115 | mu: new(sync.Mutex), |
| 116 | buf: &buf, |
| 117 | handler: jsonHandler, |
| 118 | } |
| 119 | if opts != nil { |
| 120 | lh.opts = *opts |
| 121 | } |
| 122 | return lh |
| 123 | } |
| 124 | |
| 125 | // Enabled implements [slog.Handler.Enabled] by comparing level to the [ServerSession]'s level. |
| 126 | func (h *LoggingHandler) Enabled(ctx context.Context, level slog.Level) bool { |
no outgoing calls
searching dependent graphs…