Skip to content

Latest commit

 

History

History
49 lines (40 loc) · 1.47 KB

File metadata and controls

49 lines (40 loc) · 1.47 KB

ECSLog: Slog Handler for ECS logging

godoc license

This slog handler makes structured ECS logging easier with slog by accepting the dot notation (event.action) and producing corresponding nested JSON objects. This approach eliminates the need for nested grouping when constructing the log attributes.

For details see documentation.

Features:

  • deduplication of scalar attributes
  • downstream log instances can set attribute in "group" created upstream
  • it has comparable performance to slog.NewJSONHandler()

Performance

The implementation has very similar performance to slog.NewJSONHandler(), based on some local testing with zap benchmark.

Example

logger := slog.New(ecslog.NewHandler(os.Stderr)).
    With(
        slog.String("event.dataset", "testing"),
        slog.String("log.logger", "slog"),
    )
// ...
logger.Info("Test!",
    slog.String("event.action", "test"),
)
// {
//   "@timestamp": "2026-01-11T17:00:42.42648+01:00",
//   "message": "Test!",
//   "log": {
//     "logger": "slog",
//     "level": "INFO"
//   },
//   "event": {
//     "dataset": "testing",
//     "action": "test"
//   }
// }