on99 / logrus-graylog-hook

Graylog hook for logrus

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Graylog Hook for Logrus :walrus:

Use this hook to send logs to multiple Graylog servers over UDP.

====

Build Status Go Report Card codecov

Motivation

There is no way to setup a UDP load balancer in front of a Graylog server cluster due to the chunking feature, which requires all chunks of a GELF message being sent to the same Graylog server node.

Client side load balancing provided by this logrus hook could be a solution to this issue.

Algorithm

Weighted-random algorithm is used to select Graylog server node as log destination every time when the hook is fired by logrus.

Install

This package depends on

$ go get -u github.com/on99/logrus-graylog-hook

Usage

package main

import (
	"io/ioutil"
	"time"

	"github.com/Sirupsen/logrus"
	graylog "github.com/on99/logrus-graylog-hook"
)

func main() {
	// graylog configuration
	config := graylog.NewConfig()
	config.Facility = "graylog_hook"
	config.HealthCheckInterval = 5 * time.Second // check graylog nodes health periodically
	config.StaticMeta = map[string]interface{}{  // static meta that always sent to graylog
		"go_version": "1.7.1",
	}

	// create hook
	hook := graylog.New(config)

	// set graylog nodes
	// 5/10 chances log will be sent to node-1
	// 3/10 chances log will be sent to node-2
	// 2/10 chances log will be sent to node-3
	hook.SetNodeConfigs(
		graylog.NodeConfig{
			UDPAddress:     "node-1.graylog:12201",
			HealthCheckURL: "node-1.graylog/api/system/lbstatus",
			Weight:         5,
		},
		graylog.NodeConfig{
			UDPAddress:     "node-2.graylog:12201",
			HealthCheckURL: "node-2.graylog/api/system/lbstatus",
			Weight:         3,
		},
		graylog.NodeConfig{
			UDPAddress:     "node-3.graylog:12201",
			HealthCheckURL: "node-3.graylog/api/system/lbstatus",
			Weight:         2,
		},
	)

	// start health check
	// all graylog nodes are alive by default
	hook.StartHealthCheck()

	logrus.AddHook(hook)             // add graylog hook to logrus
	logrus.SetOutput(ioutil.Discard) // discard logrus output

	// log something, enjoy a cup of coffee
	logrus.WithField("key", "value").Info("log message")
}

About

Graylog hook for logrus

License:GNU General Public License v3.0


Languages

Language:Go 100.0%