Overview
Advanced options provide extensibility and security features including hooks for processing log entries and automatic redaction of sensitive data.WithHooks
Adds hooks to the logger for processing log entries.Signature
Parameters
hooks(…Hook): Variable number of hook implementations
Returns
Option: A configuration option that registers hooks
Type Definition
Description
Hooks provide an extension point for processing log entries after they’re created but before they’re formatted and written. Common use cases:- Send critical errors to external services (Slack, PagerDuty)
- Collect metrics and statistics
- Filter or modify log entries
- Trigger alerts based on log content
- Integrate with monitoring systems
Examples
Simple Function Hook
Custom Hook Implementation
Slack Notification Hook
Multiple Hooks
Hook Execution
- Hooks are executed in the order they were registered
- If a hook returns an error, logging continues (the error is ignored)
- Hooks are called synchronously before the log entry is written
Performance
Hooks add overhead to each log call. Keep hook logic lightweight or use level filtering within the hook.WithRedactor
Enables redaction of sensitive fields.Signature
Parameters
keys(…string): Field names to redact
Returns
Option: A configuration option that enables field redaction
Type Definition
Description
Automatically masks values of specified fields to prevent sensitive data from appearing in logs. Redacted values are replaced with***.
Examples
Security Note
Redaction happens at the field level. Field names must match exactly (case-sensitive).WithCommonRedaction
Enables redaction of common sensitive fields.Signature
Returns
Option: A configuration option that redacts common sensitive fields
Description
Convenience option that automatically redacts commonly sensitive field names. This is equivalent to callingWithRedactor with a predefined list of sensitive keys.
Redacted Fields
The following field names are automatically redacted:password,passwd,pwdtoken,api_key,apikey,api-keysecret,authorization,authcookie,sessioncredit_card,ssn,social_security
Examples
Best Practices
Helper Functions
NewFuncHook
Creates a Hook from a function.Signature
Parameters
fn: Function that processes log entries
Returns
Hook: A hook implementation
Type Definition
Examples
CommonSensitiveKeys
Returns a list of commonly sensitive field names.Signature
Returns
[]string: List of common sensitive field names
Example
Combined Usage
Use Cases
Error Monitoring
Audit Logging
Compliance and Security
Related
- Hook Reference - Hook interface details
- Entry Reference - Log entry structure
- Core Options - Basic logger configuration