Overview
TheHook interface enables extensible processing of log entries. Hooks are called after an entry is formatted but before it’s written, allowing you to:
- Send logs to external systems (Slack, email, monitoring)
- Filter or modify log entries
- Collect metrics and statistics
- Trigger alerts based on log content
Hook Interface
entry- The log entry to process
error- Error if hook processing failed, nil otherwise
HookFunc Adapter
Adapter type that allows ordinary functions to be used as hooks.Constructor Functions
NewFuncHook
Creates a Hook from a function.fn- A function that processes log entries
Hook- A Hook that calls the provided function
Usage Examples
Simple Function Hook
Metrics Collection Hook
Alert Hook
Level-Filtered Hook
Field Inspection Hook
Multiple Hooks
Entry Modification Hook
Hook Execution
Hooks are executed:- After formatting: Entry has been converted to bytes
- Before writing: Output hasn’t been written yet
- Synchronously: Each hook runs in sequence
- With error handling: Hook errors are logged but don’t prevent other hooks
Entry Methods for Hooks
Hooks have access to the full Entry API:Best Practices
Built-in Hooks
go_logs provides several built-in hooks:- SlackHook - Send logs to Slack channels
Related
- SlackHook - Built-in Slack integration
- Custom Hooks - Guide to creating custom hooks
- Logger Options - WithHooks option