Handle handles the given record.
(ctx context.Context, record slog.Record)
| 28 | |
| 29 | // Handle handles the given record. |
| 30 | func (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. |
| 75 | func (s *SlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { |
nothing calls this directly
no test coverage detected