graylog

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2015 License: MIT Imports: 9 Imported by: 0

README

Graylog Hook for Logrus  Build Status godoc reference

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).
  • A facility
  • an optional hash with extra global fields. These fields will be included in all messages sent to Graylog
import (
    "log/syslog"
    "github.com/Sirupsen/logrus"
    "gopkg.in/gemnasium/logrus-graylog-hook.v1"
    )

func main() {
    log := logrus.New()
    hook := graylog.NewGraylogHook("<graylog_ip>:<graylog_port>", "some_facility", map[string]interface{}{"this": "is logged every time"})
    log.Hooks.Add(hook)
    log.Info("some logging message")
}
Asynchronous logger
import (
    "log/syslog"
    "github.com/Sirupsen/logrus"
    "gopkg.in/gemnasium/logrus-graylog-hook.v1"
    )

func main() {
    log := logrus.New()
    hook := graylog.NewAsyncGraylogHook("<graylog_ip>:<graylog_port>", "some_facility", map[string]interface{}{"this": "is logged every time"})
    defer hook.Flush()
    log.Hooks.Add(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.Hooks.Add(graylog.NewGraylogHook(graylogAddr, "api", map[string]interface{}{})) // set graylogAddr accordingly
log.SetFormatter(new(NullFormatter)) // Don't send logs to stdout

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BufSize uint = 8192

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.

Functions

This section is empty.

Types

type GraylogHook

type GraylogHook struct {
	Facility string
	Extra    map[string]interface{}
	// contains filtered or unexported fields
}

GraylogHook to send logs to a logging service compatible with the Graylog API and the GELF format.

func NewAsyncGraylogHook added in v1.1.0

func NewAsyncGraylogHook(addr string, facility string, extra map[string]interface{}) *GraylogHook

NewAsyncGraylogHook creates a hook to be added to an instance of logger. The hook created will be asynchronous, and it's the responsability of the user to call the Flush method to empty the log queue.

func NewGraylogHook

func NewGraylogHook(addr string, facility string, extra map[string]interface{}) *GraylogHook

NewGraylogHook creates a hook to be added to an instance of logger.

func (*GraylogHook) Fire

func (hook *GraylogHook) Fire(entry *logrus.Entry) error

Fire is called when a log event is fired. We assume the entry will be altered by another hook, otherwise we might logging something wrong to Graylog

func (*GraylogHook) Flush added in v1.1.0

func (hook *GraylogHook) Flush()

Flush waits for the log queue to be empty. This func is meant to be used when the hook was created with NewAsyncGraylogHook.

func (*GraylogHook) Levels

func (hook *GraylogHook) Levels() []logrus.Level

Levels returns the available logging levels.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL