kafkalogrus

package module
v0.0.0-...-e8382a4 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

README

Kafka Logrus hook

Modified version of Kafka hook for Logrus. Just need hook for my projects and I couldn't see that somebody actively supports any related projects.

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

How to use

Import package
import kl "github.com/tracer0tong/kafkalogrus"
Create a hook (KafkaHook)
NewKafkaLogrusHook(id string, 
                   levels []logrus.Level, 
                   formatter logrus.Formatter, 
                   brokers []string, 
                   defaultTopic string, 
                   injectHostname bool) (*KafkaHook, error)
  • id: hook identifier
  • levels: logrus.Levels supported by the hook
  • formatter: logrus.Formatter used by the hook
  • brokers: Kafka brokers
  • defaultTopic: default Kafka topic for messages
  • injectHostname: if true, will inject os.Hostname() to each request

For example:

hook, err := kl.NewKafkaHook(
        "klh",
        []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"},
        "test",
        true
    )
Create a logrus.Logger

For example:

logger := logrus.New()
Add hook to logger
logger.Hooks.Add(hook)
Modify topic
l := logger.WithField("topic", "nondefaulttopic")

The field name must be topic.

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/tracer0tong/kafkalogrus/tree/master/examples

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KafkaLogrusHook

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

KafkaLogrusHook

func NewKafkaLogrusHook

func NewKafkaLogrusHook(id string,
	levels []logrus.Level,
	formatter logrus.Formatter,
	brokers []string,
	defaultTopic string,
	injectHostname bool) (*KafkaLogrusHook, error)

Create a new KafkaHook

func (*KafkaLogrusHook) Fire

func (hook *KafkaLogrusHook) Fire(entry *logrus.Entry) error

func (*KafkaLogrusHook) Id

func (hook *KafkaLogrusHook) Id() string

func (*KafkaLogrusHook) Levels

func (hook *KafkaLogrusHook) Levels() []logrus.Level

func (*KafkaLogrusHook) SetHostname

func (k *KafkaLogrusHook) SetHostname(hostname string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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