sockrus

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2023 License: GPL-3.0 Imports: 4 Imported by: 1

README

Socket hook for logrus

GoDoc

Use this hook to send the logs over UDP, TCP or Unix socket.

Output format is JSON, formatted for Logstash/ElasticSearch.

Usage

package main

import (
        "github.com/sirupsen/logrus"
        "github.com/Showmax/sockrus"
)

func main() {
        log := logrus.New()
        hook, err := sockrus.NewHook("unixpacket", "/tmp/log.sock")
        if err != nil {
                log.Fatal(err)
        }
        log.Hooks.Add(hook)
        ctx := log.WithFields(logrus.Fields{
          "method": "main",
        })
        ...
        ctx.Info("Hello World!")
}

This is how it will look like:

{
  "@timestamp": "2016-04-15T12:49:36Z",
  "@version": 1,
  "level": "info",
  "message": "Hello World!",
  "method": "main"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSockrus

func NewSockrus(config Config) (*logrus.Logger, *logrus.Entry)

NewSockrus is a wrapper for initialization of logrus with sockrus hook. It returns new instance of logrus.Logger and logrus.Entry. All errors are fatal.

Types

type Config

type Config struct {
	Hostname       string       // Hostname of the machine we are logging from.
	LogLevel       logrus.Level // Log level of messages we want to send to socket.
	Service        string       // Service that is creating the logs.
	SocketAddr     string       // Address of the socket.
	SocketProtocol string       // Protocol of the socket.
	AddNewLine     bool         // Toggle to send newline after every message to socket.
}

Config serves as means to configure logger and hook.

type Hook

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

Hook represents a connection to a socket

func NewHook

func NewHook(protocol, address string, addNewline bool) (*Hook, error)

NewHook establish a socket connection. Protocols allowed are: "udp", "tcp", "unix" (corresponds to SOCK_STREAM), "unixdomain" (corresponds to SOCK_DGRAM) or "unixpacket" (corresponds to SOCK_SEQPACKET).

For TCP and UDP, address must have the form `host:port`.

For Unix networks, the address must be a file system path.

func (*Hook) Fire

func (h *Hook) Fire(entry *logrus.Entry) error

Fire send log to the defined socket

func (*Hook) Levels

func (h *Hook) Levels() []logrus.Level

Levels return an array of handled logging levels

Jump to

Keyboard shortcuts

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