Logrus Hooks
Logrus is a popular structured logger that allows to add hooks that are invoked for every message. These hooks can be used to send the messages to remote tracking and reporting system. Things may go wrong when talking to remote systems, like delays and errors.
The hooks from this package are decorators that help to deal with:
- Transient errors
- Excessive logging messages
- Slow transmission times
Setup
The examples will use the syslog hook to send messages to syslog daemon. The setup looks like this:
import (
"log/syslog"
"github.com/misho-kr/logrus-hooks"
"github.com/sirupsen/logrus"
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
)
func init() {
hook, err := NewSyslogHook("udp", "server.fqdn:514", syslog.LOG_DEBUG, "")
}
From here additional hooks can be added for enhanced logging functionality.
Retry with backoff
Prevent transient errors from procesing the log messages with retries
log.AddHook(RetryHook(
hook,
100 * time.Millisecond,
hooks.Retries(3),
hooks.FactorPct(100),
hooks.JitterPct(10),
))
Rate limits
Put a cap on the number of logging messages per time interval
log.AddHook(RateLimitHook(
hook,
hooks.PerSecond(10),
hooks.Burst(20),
))
Asynchronous execution
Fire the hook in separate goroutine to avoid blocking the logger and main application
log.AddHook(AsyncHook(
hook,
hooks.Senders(10),
hooks.BoostSenders(20),
))