gelf

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: MIT Imports: 17 Imported by: 0

README

go-gelf - GELF Library and Writer for Go

GELF (Graylog Extended Log Format) is an application-level logging protocol that avoids many of the shortcomings of syslog. While it can be run over any stream or datagram transport protocol, it has special support (chunking) to allow long messages to be split over multiple datagrams.

Versions

This was forked from Graylog2 go-gelf to add the GELF HTTP transport protocol.

Currently sending GELF is supported via UDP, TCP and HTTP. TLS is experimental in the tls branch.

The library provides an API that applications can use to log messages directly to a Graylog server and an io.Writer that can be used to redirect the standard library's log messages (os.Stdout) to a Graylog server.

Installing

To install, run:

go get xiam.li/gelf

Usage

The easiest way to integrate graylog logging into your go app is by having your main function (or even init) call log.SetOutput(). By using an io.MultiWriter, we can log to both stdout and graylog - giving us both centralized and local logs. (Redundancy is nice).

package main

import (
	"flag"
	"io"
	"log"
	"os"
	"xiam.li/gelf"
)

func main() {
	var graylogAddr string

	flag.StringVar(&graylogAddr, "graylog", "", "graylog server addr")
	flag.Parse()

	if graylogAddr != "" {
		// If using UDP
		gelfWriter, err := gelf.NewUDPWriter(graylogAddr)
		// If using TCP
		//gelfWriter, err := gelf.NewTCPWriter(graylogAddr)
		if err != nil {
			log.Fatalf("gelf.NewWriter: %s", err)
		}
		// log to both stderr and graylog2
		log.SetOutput(io.MultiWriter(os.Stderr, gelfWriter))
		log.Printf("logging to stderr & graylog2@'%s'", graylogAddr)
	}

	// From here on out, any calls to log.Print* functions
	// will appear on stdout, and be sent over UDP or TCP to the
	// specified Graylog2 server.

	log.Printf("Hello gray World")

	// ...
}

The above program can be invoked as:

go run test.go -graylog=localhost:12201

When using UDP messages may be dropped or re-ordered. However, Graylog server availability will not impact application performance; there is a small, fixed overhead per log call regardless of whether the target server is reachable or not.

To Do

  • WriteMessage example

License

go-gelf is offered under the MIT license, see LICENSE for details.

Documentation

Index

Constants

View Source
const (
	LOG_EMERG   = 0
	LOG_ALERT   = 1
	LOG_CRIT    = 2
	LOG_ERR     = 3
	LOG_WARNING = 4
	LOG_NOTICE  = 5
	LOG_INFO    = 6
	LOG_DEBUG   = 7
)

Syslog severity levels

View Source
const (
	DefaultMaxReconnect   = 3
	DefaultReconnectDelay = 1
)
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?

Variables

This section is empty.

Functions

func ProcessLog added in v1.0.1

func ProcessLog(hostname, facility string, log []byte) (message []byte, err error)

func ProcessMessage added in v1.0.1

func ProcessMessage(msg *Message) (message []byte, err 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
	CompressNone
)

type GelfWriter

type GelfWriter struct {
	Facility string // defaults to current process name
	// contains filtered or unexported fields
}

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

func (*GelfWriter) Close

func (w *GelfWriter) Close() error

Close connection and interrupt blocked Read or Write operations

type HTTPWriter

type HTTPWriter struct {
	GelfWriter

	MaxReconnect   int
	ReconnectDelay time.Duration
	// contains filtered or unexported fields
}

func NewHTTPWriter

func NewHTTPWriter(proto, addr string) (*HTTPWriter, error)

func (*HTTPWriter) Write

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

func (*HTTPWriter) WriteMessage

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

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

func (*HTTPWriter) WriteRaw added in v1.0.1

func (w *HTTPWriter) WriteRaw(messageBytes []byte) error

type Message

type Message struct {
	Version  string                 `json:"version"`
	Host     string                 `json:"host"`
	Short    string                 `json:"short_message"`
	Full     string                 `json:"full_message,omitempty"`
	Time     string                 `json:"timestamp,omitzero"`
	Level    int32                  `json:"level,omitempty"`
	Facility string                 `json:"facility,omitempty"`
	Extra    map[string]interface{} `json:"-"`
	RawExtra json.RawMessage        `json:"-"`
}

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

func (*Message) MarshalJSONBuf

func (m *Message) MarshalJSONBuf(buf *bytes.Buffer) error

func (*Message) UnmarshalJSON

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

type Reader

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

func NewReader

func NewReader(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)

type TCPReader

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

func (*TCPReader) Close

func (r *TCPReader) Close()

type TCPWriter

type TCPWriter struct {
	GelfWriter

	MaxReconnect   int
	ReconnectDelay time.Duration
	// contains filtered or unexported fields
}

func NewTCPWriter

func NewTCPWriter(addr string) (*TCPWriter, error)

func (*TCPWriter) Write

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

func (*TCPWriter) WriteMessage

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

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

func (*TCPWriter) WriteRaw added in v1.0.1

func (w *TCPWriter) WriteRaw(messageBytes []byte) error

type UDPWriter

type UDPWriter struct {
	GelfWriter
	CompressionLevel int // one of the consts from compress/flate
	CompressionType  CompressType
}

func NewUDPWriter

func NewUDPWriter(addr string) (*UDPWriter, error)

New returns a new GELF Writer. 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()

func (*UDPWriter) Write

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

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

func (*UDPWriter) WriteMessage

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

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

func (*UDPWriter) WriteRaw added in v1.0.1

func (w *UDPWriter) WriteRaw(messageBytes []byte) (err error)

type Writer

type Writer interface {
	Close() error
	Write([]byte) (int, error)
	WriteMessage(*Message) error
	WriteRaw([]byte) error
}

Jump to

Keyboard shortcuts

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