GitHub - samber/slog-mock: 🚨 slog: mock handler (original) (raw)
slog: Mock handler
A mock Handler for slog Go library. Used for testing purpose.
See also:
- slog-multi:
slog.Handlerchaining, fanout, routing, failover, load balancing... - slog-formatter:
slogattribute formatting - slog-sampling:
slogsampling policy - slog-mock:
slog.Handlerfor test purposes
HTTP middlewares:
- slog-gin: Gin middleware for
sloglogger - slog-echo: Echo middleware for
sloglogger - slog-fiber: Fiber middleware for
sloglogger - slog-chi: Chi middleware for
sloglogger - slog-http:
net/httpmiddleware forsloglogger
Loggers:
- slog-zap: A
sloghandler forZap - slog-zerolog: A
sloghandler forZerolog - slog-logrus: A
sloghandler forLogrus
Log sinks:
- slog-datadog: A
sloghandler forDatadog - slog-betterstack: A
sloghandler forBetterstack - slog-rollbar: A
sloghandler forRollbar - slog-loki: A
sloghandler forLoki - slog-sentry: A
sloghandler forSentry - slog-syslog: A
sloghandler forSyslog - slog-logstash: A
sloghandler forLogstash - slog-fluentd: A
sloghandler forFluentd - slog-graylog: A
sloghandler forGraylog - slog-quickwit: A
sloghandler forQuickwit - slog-slack: A
sloghandler forSlack - slog-telegram: A
sloghandler forTelegram - slog-mattermost: A
sloghandler forMattermost - slog-microsoft-teams: A
sloghandler forMicrosoft Teams - slog-webhook: A
sloghandler forWebhook - slog-kafka: A
sloghandler forKafka - slog-nats: A
sloghandler forNATS - slog-parquet: A
sloghandler forParquet+Object Storage - slog-channel: A
sloghandler for Go channels
🚀 Install
go get github.com/samber/slog-mock
Compatibility: go >= 1.21
No breaking changes will be made to exported APIs before v1.0.0.
💡 Usage
GoDoc: https://pkg.go.dev/github.com/samber/slog-mock
Handler options
type Option struct { // optional Enabled func(ctx context.Context, level slog.Level) bool // optional Handle func(ctx context.Context, record slog.Record) error }
Example
import ( "fmt" "net/http" "time"
slogmock "github.com/samber/slog-mock"
"log/slog")
func main() { logger := slog.New( slogmock.Option{ Handle: func(ctx context.Context, record slog.Record) error { // simulate slowdown then error from log sink time.Sleep(100*time.Millisecond) panic("could not connect to datadog") }, }.NewMockHandler(), ) logger = logger.With("release", "v1.0.0")
logger.
With(
slog.Group("user",
slog.String("id", "user-123"),
slog.Time("created_at", time.Now()),
),
).
With("error", fmt.Errorf("an error")).
Error("a message", slog.Int("count", 1))}
🤝 Contributing
- Ping me on Twitter @samuelberthe (DMs, mentions, whatever :))
- Fork the project
- Fix open issues or request new features
Don't hesitate ;)
Install some dev dependencies
make tools
Run tests
make test
or
make watch-test
👤 Contributors
💫 Show your support
Give a ⭐️ if this project helped you!
📝 License
Copyright © 2023 Samuel Berthe.
This project is MIT licensed.