Overview
SlackHook sends log entries to Slack channels based on a configurable level threshold. Only logs at or above the threshold are sent, preventing spam from debug/info messages.
This hook integrates with the existing SlackNotifier adapter and provides level-based filtering to ensure only important messages reach your Slack channels.
Type Definition
SlackNotifier Interface
Constructor Function
NewSlackHook
Creates a new Slack hook with the given notifier and level threshold.notifier- The SlackNotifier to use for sending messageslevel- The minimum log level to send to Slack (e.g., ErrorLevel)
*SlackHook- A configured Hook that sends logs to Slack
Methods
Run
Implements Hook.Run by sending the log entry to Slack if it meets the level threshold.entry- The log entry to potentially send to Slack
error- Error if sending to Slack failed, nil otherwise
- Returns nil immediately if entry level is below threshold (fast-path)
- Formats the message using
formatMessage - Sends to Slack using the notifier
GetLevel
Returns the minimum log level that will be sent to Slack.go_logs.Level- The current level threshold
SetLevel
Changes the minimum log level threshold dynamically.level- The new minimum level to send to Slack
Message Format
SlackHook formats messages as:Usage Examples
Basic Setup
Multiple Channels with Different Thresholds
Dynamic Level Adjustment
With Custom Notifier (Mock for Testing)
Graceful Degradation
Environment-Based Configuration
Configuration
SlackHook requires SlackNotifier to be configured with: Environment variables:Best Practices
Performance
SlackHook is designed for minimal performance impact:- Fast-path filtering: Level check uses integer comparison (~1ns)
- Conditional execution: Only formats/sends if level matches
- No allocations: Level check has zero allocations
Message Formatting Details
TheformatMessage method creates Slack messages:
Related
- Hook Interface - Base hook interface
- Custom Hooks - Creating custom hooks
- SlackNotifier - Slack adapter documentation
- Logger Options - WithHooks option