graylog

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2023 License: MIT Imports: 19 Imported by: 0

README

graylog logger

Package graylog provides support for logging to the Graylog server.

It can send messages to the Graylog server using UDP or TCP. When using UDP as a transport layer, the messages sent are gzip compressed and automatically chunked.

import (
	"time"
	"github.com/mdigger/graylog"
	"golang.org/x/exp/slog"
)

func main() {
    // init graylog logger
    log, err := graylog.Dial("udp", "localhost:12201")
    if err != nil {
        panic(err)
    }
    defer log.Close()

    // send debug message with attributes
    log.Debug("Test message.\nMore info...",
        slog.Any("log", log),
        slog.Bool("bool", true),
        slog.Time("now", time.Now()),
        slog.Group("group",
            slog.String("str", "string value"),
            slog.Duration("duration", time.Hour/3)),
        slog.Any("object", struct {
            Text string `json:"text"`
        }{Text: "text"}),
    )

    // register as default
    slog.SetDefault(log.Logger)
}

Documentation

Overview

Package buffer provides a pool-allocated byte buffer with custom methods.

Package graylog provides support for logging to the Graylog server.

It can send messages to the Graylog server using UDP or TCP. When using UDP as a transport layer, the messages sent are gzip compressed and automatically chunked.

Example
package main

import (
	"time"

	"github.com/mdigger/graylog"
	"golang.org/x/exp/slog"
)

func main() {
	// init graylog logger
	log, err := graylog.Dial("udp", "localhost:12201")
	if err != nil {
		panic(err)
	}
	defer log.Close()

	// send info message with attributes
	log.Info("Test message.\nMore info...",
		slog.Any("log", log),
		slog.Bool("bool", true),
		slog.Time("now", time.Now()),
		slog.Group("group",
			slog.String("str", "string value"),
			slog.Duration("duration", time.Hour/3)),
		slog.Any("struct", struct {
			Test string `json:"test"`
		}{Test: "test"}),
	)

	// register as default
	slog.SetDefault(log.Logger)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrMessageToLarge = errors.New("message too large")

ErrMessageToLarge returns when trying to send too long message other UDP.

Functions

func SetFacility added in v0.4.0

func SetFacility(s string)

SetFacility set Facility log filed used when generating a log message in GELF format. By default, it is filled in during initialization using the path to the main module of the application.

If you want to change this value, then this must be done before initializing the log.

Types

type Logger

type Logger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

Logger is an io.Logger for sending log messages to the Graylog server.

func Dial

func Dial(network, address string, attrs ...slog.Attr) (*Logger, error)

Dial establishes a connection to the Graylog server and returns Logger to send log messages.

Supported networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only), "udp6" (IPv6-only). When using UDP as a transport layer, the messages sent are compressed using GZIP and automatically chunked.

Attrs specify a list of attributes that will be added to all messages sent to Graylog server.

func (Logger) Close

func (w Logger) Close() error

Close closes a connection to the Graylog server.

func (Logger) LogValue

func (w Logger) LogValue() slog.Value

LogValue return log value with connection info (network & address).

Jump to

Keyboard shortcuts

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