slog: Slack handler
A Slack Handler for slog Go library.
See also:
🚀 Install
go get github.com/samber/slog-slack
Compatibility: go >= 1.21
No breaking changes will be made to exported APIs before v2.0.0.
💡 Usage
GoDoc: https://pkg.go.dev/github.com/samber/slog-slack
Handler options
type Option struct {
// log level (default: debug)
Level slog.Leveler
// slack webhook url
WebhookURL string
// slack bot token
BotToken string
// slack channel (default: webhook channel)
Channel string
// bot username (default: webhook username)
Username string
// bot emoji (default: webhook emoji)
IconEmoji string
// bot emoji (default: webhook emoji)
IconURL string
// optional: customize Slack message builder
Converter Converter
}
Attributes will be injected in message attachments.
Example
Using webhook
Generate a webhook here.
import (
slogslack "github.com/samber/slog-slack"
"log/slog"
)
func main() {
webhook := "https://hooks.slack.com/services/xxx/yyy/zzz"
channel := "alerts"
logger := slog.New(slogslack.Option{Level: slog.LevelError, WebhookURL: webhook, Channel: channel}.NewSlackHandler())
logger = logger.
With("environment", "dev").
With("release", "v1.0.0")
// log error
logger.
With("category", "sql").
With("query.statement", "SELECT COUNT(*) FROM users;").
With("query.duration", 1*time.Second).
With("error", fmt.Errorf("could not count users")).
Error("caramba!")
// log user signup
logger.
With(
slog.Group("user",
slog.String("id", "user-123"),
slog.Time("created_at", time.Now()),
),
).
Info("user registration")
}
Using bot token
Use Bot token.
import (
slogslack "github.com/samber/slog-slack"
"log/slog"
)
func main() {
token := "xoxb-"
channel := "alerts"
logger := slog.New(slogslack.Option{Level: slog.LevelError, BotToken: token, Channel: channel}.NewSlackHandler())
logger = logger.
With("environment", "dev").
With("release", "v1.0.0")
}
🤝 Contributing
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.