Skip to content

Instantly share code, notes, and snippets.

@trashhalo
Created April 14, 2025 20:49
Show Gist options
  • Save trashhalo/ec3af1d97c7a29deaa00adbaba0d1c2c to your computer and use it in GitHub Desktop.
Save trashhalo/ec3af1d97c7a29deaa00adbaba0d1c2c to your computer and use it in GitHub Desktop.
func temporalLoggerHandler(logger log.Logger) slog.Handler {
return slogmulti.NewHandleInlineHandler(
func(ctx context.Context, groups []string, attrs []slog.Attr, record slog.Record) error {
// Build message and attributes
msg := record.Message
// Convert attributes to keyvals format
var keyvals []interface{}
// Add provided attributes (from WithAttrs calls)
for _, attr := range attrs {
keyvals = append(keyvals, attr)
}
// Add record attributes
record.Attrs(func(attr slog.Attr) bool {
keyvals = append(keyvals, attr)
return true
})
// Forward to appropriate method based on level
switch {
case record.Level <= slog.LevelDebug:
logger.Debug(msg, keyvals...)
case record.Level <= slog.LevelInfo:
logger.Info(msg, keyvals...)
case record.Level <= slog.LevelWarn:
logger.Warn(msg, keyvals...)
default:
logger.Error(msg, keyvals...)
}
return nil
},
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment