Created
April 14, 2025 20:49
-
-
Save trashhalo/ec3af1d97c7a29deaa00adbaba0d1c2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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