# README
Graylog Hook for Logrus

Use this hook to send your logs to Graylog server over UDP. The hook is non-blocking: even if UDP is used to send messages, the extra work should not block the logging function.
All logrus fields will be sent as additional fields on Graylog.
Usage
The hook must be configured with:
- A Graylog GELF UDP address (a "ip:port" string).
- an optional hash with extra global fields. These fields will be included in all messages sent to Graylog
package main
import (
"log/syslog"
log "github.com/sirupsen/logrus"
"gopkg.in/gemnasium/logrus-graylog-hook.v2"
)
func main() {
hook := graylog.NewGraylogHook("<graylog_ip>:<graylog_port>", map[string]interface{}{"this": "is logged every time"})
log.AddHook(hook)
log.Info("some logging message")
}
Asynchronous logger
package main
import (
"log/syslog"
log "github.com/sirupsen/logrus"
"gopkg.in/gemnasium/logrus-graylog-hook.v2"
)
func main() {
hook := graylog.NewAsyncGraylogHook("<graylog_ip>:<graylog_port>", map[string]interface{}{"this": "is logged every time"})
defer hook.Flush()
log.AddHook(hook)
log.Info("some logging message")
}
Disable standard logging
For some reason, you may want to disable logging on stdout, and keep only the messages in Graylog (ie: a webserver inside a docker container).
You can redirect stdout
to /dev/null
, or just not log anything by creating a NullFormatter
implementing logrus.Formatter
interface:
type NullFormatter struct {
}
// Don't spend time formatting logs
func (NullFormatter) Format(e *log.Entry) ([]byte, error) {
return []byte{}, nil
}
And set this formatter as the new logging formatter:
log.Infof("Log messages are now sent to Graylog (udp://%s)", graylogAddr) // Give a hint why logs are empty
log.AddHook(graylog.NewGraylogHook(graylogAddr, "api", map[string]interface{}{})) // set graylogAddr accordingly
log.SetFormatter(new(NullFormatter)) // Don't send logs to stdout
# Functions
NewAsyncGraylogHook creates a hook to be added to an instance of logger.
NewGraylogHook creates a hook to be added to an instance of logger.
No description provided by the author
New returns a new GELF Writer.
# Constants
Used to control GELF chunking.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
Set graylog.BufSize = <value> _before_ calling NewGraylogHook Once the buffer is full, logging will start blocking, waiting for slots to be available in the queue.
# Structs
GraylogHook to send logs to a logging service compatible with the Graylog API and the GELF format.
Message represents the contents of the GELF message.
No description provided by the author
Writer implements io.Writer and is used to send both discrete messages to a graylog2 server, or data from a stream-oriented interface (like the functions in log).
# Type aliases
What compression type the writer should use when sending messages to the graylog2 server.