Skip to content
Michael Kenney edited this page Aug 13, 2018 · 3 revisions

Hooks

You can add hooks for logging levels. For example to send errors to an exception tracking service on Error, Fatal and Panic, info to StatsD or log to multiple places simultaneously, e.g. syslog.

bdlm/log comes with built-in hooks. Add those, or your custom hook, in init:

import (
    log "github.com/bdlm/log"
    log_syslog "github.com/bdlm/log/hooks/syslog"
    "log/syslog"
)

func init() {
    // Use the Airbrake hook to report errors that have Error severity or above to
    // an exception tracker. You can create custom hooks, see the Hooks section.
    log.AddHook(airbrake.NewHook(123, "xyz", "production"))

    hook, err := log_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
    if err != nil {
        log.Error("Unable to connect to local syslog daemon")
    } else {
        log.AddHook(hook)
    }
}

Note: Syslog hook also supports connecting to local system logger (Ex. /dev/log or /var/run/syslog or /var/run/log). For details please see the syslog hook README.