Categorygithub.com/carlcarl/logrus-kafka-hook
modulepackage
0.0.0-20240909024231-2e02f5cc0631
Repository: https://github.com/carlcarl/logrus-kafka-hook.git
Documentation: pkg.go.dev

# README

logkafka

Status

GoDoc GoCard coverage Build Status

import "github.com/kenjones-cisco/logrus-kafka-hook"

Package logkafka provides a Logrus Hook implementation for publishing log messages on a Kafka Topic.

logkafka uses a builder pattern to configure the Hook. This provides simplicity and flexibility when configuring a Hook. There are convenience functions for creating a sarama.AsyncProducer and logrus.Formatter.

Basic Usage

// use SimpleProducer to create an AsyncProducer
producer := logkafka.SimpleProducer([]string{"127.0.0.1:9092"}, sarama.CompressionSnappy, sarama.WaitForLocal, nil)

// use DefaultFormatter to create a JSONFormatter with pre-defined fields or override with any fields
// create the Hook and use the builder functions to apply configurations
hook := logkafka.New().WithFormatter(logkafka.DefaultFormatter(logrus.Fields{"type": "myappName"})).WithProducer(producer)

// create a new logger and add the hook
log := logrus.New()
log.Hooks.Add(hook)

log.Debug("Hello World")

Example of formatted message published to Kafka

{
	"@timestamp": "2018-04-20T04:03:00Z",
	"@version": "1",
	"level": "info",
	"message": "Hello World",
	"type": "myappName"
}

# Functions

DefaultFormatter returns a default structured formatter: A JSON format with "@version" set to "1" (unless set differently in `fields`, "type" to "log" (unless set differently in `fields`), "@timestamp" to the log time and "message" to the log message.
New creates a new logrus hook for Kafka.
SimpleProducer accepts a minimal set of configurations and creates an AsyncProducer.

# Constants

RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to ensure the formatted time is always the same number of characters.

# Variables

ErrNoProducer is used when hook Fire method is called and no producer was configured.

# Structs

Hook represents a logrus hook for Kafka.
StructuredFormatter represents a structured format.