logrus-kafka-hook

A logrus.Hook which sends a single log entry to multiple kafka topics simultaneously.

How to use

Import package

import lkh "github.com/gfremex/logrus-kafka-hook"

Create a hook (KafkaHook)

NewKafkaHook(id string, levels []logrus.Level, formatter logrus.Formatter, brokers []string) (*KafkaHook, error)

For example:

hook, err := lkh.NewKafkaHook(
		"kh",
		[]logrus.Level{logrus.InfoLevel, logrus.WarnLevel, logrus.ErrorLevel},
		&logrus.JSONFormatter{},
		[]string{"192.168.60.5:9092", "192.168.60.6:9092", "192.168.60.7:9092"},
	)

Create a logrus.Logger

For example:

logger := logrus.New()

Add hook to logger

logger.Hooks.Add(hook)

Add topics

l := logger.WithField("topics", []string{"topic_1", "topic_2", "topic_3"})

The field name must be topics.

If only one topic needed, then

l := logger.WithField("topics", []string{"topic_1"})

Send messages to logger

For example:

l.Debug("This must not be logged")

l.Info("This is an Info msg")

l.Warn("This is a Warn msg")

l.Error("This is an Error msg")

Complete examples

https://github.com/gfremex/logrus-kafka-hook/tree/master/examples