graylog

package module
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 18 Imported by: 30

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).
  • an optional hash with extra global fields. These fields will be included in all messages sent to Graylog
package main

import (
    log "github.com/sirupsen/logrus"
    "github.com/gemnasium/logrus-graylog-hook/v3"
    )

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 "github.com/sirupsen/logrus"
    "github.com/gemnasium/logrus-graylog-hook/v3"
    )

func main() {
    hook := graylog.NewAsyncGraylogHook("<graylog_ip>:<graylog_port>", map[string]interface{}{"this": "is logged every time"})
    // NOTE: you must call Flush() before your program exits to ensure ALL of your logs are sent.
    // This defer statement will not have that effect if you write it in a non-main() method.
    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

Documentation

Index

Constants

View Source
const (
	ChunkSize = 1420
)

Used to control GELF chunking. Should be less than (MTU - len(UDP header)).

TODO: generate dynamically using Path MTU Discovery?

View Source
const StackTraceKey = "_stacktrace"

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

func NewTCPReader added in v3.2.0

func NewTCPReader(addr string) (*net.TCPListener, error)

Types

type CompressType

type CompressType int

What compression type the writer should use when sending messages to the graylog2 server

const (
	CompressGzip CompressType = iota
	CompressZlib
	NoCompress
)

type GELFWriter added in v3.1.0

type GELFWriter interface {
	WriteMessage(m *Message) (err error)
}

func NewWriter

func NewWriter(addr string) (GELFWriter, error)

NewWriter returns a new GELFWriter. This writer can be used to send the output of the standard Go log functions to a central GELF server by passing it to log.SetOutput()

type GraylogHook

type GraylogHook struct {
	Extra map[string]interface{}
	Host  string
	Level logrus.Level
	// contains filtered or unexported fields
}

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

func NewAsyncGraylogHook

func NewAsyncGraylogHook(addr 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 responsibility of the user to call the Flush method before exiting to empty the log queue.

func NewGraylogHook

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

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

func (*GraylogHook) Blacklist

func (hook *GraylogHook) Blacklist(b []string)

Blacklist create a blacklist map to filter some message keys. This useful when you want your application to log extra fields locally but don't want graylog to store them.

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 be logging something wrong to Graylog

func (*GraylogHook) Flush

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.

func (*GraylogHook) SetWriter

func (hook *GraylogHook) SetWriter(w *LowLevelProtocolWriter) error

SetWriter sets the hook Gelf writer

func (*GraylogHook) Writer

func (hook *GraylogHook) Writer() GELFWriter

Writer returns the writer

type HTTPWriter added in v3.1.0

type HTTPWriter struct {
	// contains filtered or unexported fields
}

HTTPWriter implements the GELFWriter interface, and cannot be used as an io.Writer

func (HTTPWriter) WriteMessage added in v3.1.0

func (h HTTPWriter) WriteMessage(m *Message) (err error)

type LowLevelProtocolWriter added in v3.2.0

type LowLevelProtocolWriter struct {
	Facility         string // defaults to current process name
	CompressionLevel int    // one of the consts from compress/flate
	CompressionType  CompressType
	// contains filtered or unexported fields
}

LowLevelProtocolWriter 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).

func (*LowLevelProtocolWriter) Write added in v3.2.0

func (w *LowLevelProtocolWriter) Write(p []byte) (n int, err error)

Write encodes the given string in a GELF message and sends it to the server specified in NewWriter().

func (*LowLevelProtocolWriter) WriteMessage added in v3.2.0

func (w *LowLevelProtocolWriter) WriteMessage(m *Message) (err error)

WriteMessage sends the specified message to the GELF server specified in the call to NewWriter(). It assumes all the fields are filled out appropriately. In general, clients will want to use Write, rather than WriteMessage.

type Message

type Message struct {
	Version  string                 `json:"version"`
	Host     string                 `json:"host"`
	Short    string                 `json:"short_message"`
	Full     string                 `json:"full_message"`
	TimeUnix float64                `json:"timestamp"`
	Level    int32                  `json:"level"`
	Facility string                 `json:"facility"`
	File     string                 `json:"file"`
	Line     int                    `json:"line"`
	Extra    map[string]interface{} `json:"-"`
}

Message represents the contents of the GELF message. It is gzipped before sending.

func (*Message) MarshalJSON

func (m *Message) MarshalJSON() ([]byte, error)

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(data []byte) error

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

func NewUDPReader added in v3.2.0

func NewUDPReader(addr string) (*Reader, error)

func (*Reader) Addr

func (r *Reader) Addr() string

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

FIXME: this will discard data if p isn't big enough to hold the full message.

func (*Reader) ReadMessage

func (r *Reader) ReadMessage() (*Message, error)

Jump to

Keyboard shortcuts

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