klog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2022 License: MIT Imports: 10 Imported by: 0

README

klog

klog provides logging to stdout and kafka by wrapping zerolog and sarama via zerolog.LevelWriter. Every log event is published to kafka. The logger will automatically fall back to stdout logger if kafka is unreachable.

klog uses diode writer which is Thread-safe, lock-free and non-blocking writer.

Logging to kafka is usefully when you want to have multiple loging backends while you can analyze your logs in realtime consuming messages. Even more there are already written Kafka Connectors that do this for you. Using Confluentinc Elasticsearch Connect you can pipe your logs to Elasticsearch from Kafka topics. There is an example on how to do that

This library tries to abstract most of the configuration and be import&go while it exposes everything that you can tweak in both zerolog and sarama if you decide to.

Basic usage
package main

import (
	"github.com/ihatemodels/klog"
)

func main() {

	log := klog.Logger{
		Application:          "kafka-logging-test",
		Environment:          "development",
		KafkaBrokers:         []string{"kafka-bootstrap.svc.cluster.local:9094"},
		KafkaTopic:           "kafka-logging-test",
		KafkaProtocolVersion: "2.7.0",
	}

    // It will not error on kafka error. So every error here should terminate the program.
	if err := log.Init(); err != nil {
		panic(err)
	}

	log.Info().
		Msg("application started")
}
go run main.go

assets assets

This package is under development, and it's not production ready yet

Documentation

Overview

Package klog provides logging to stdout and kafka by wrapping zerolog and sarama via zerolog.LevelWriter. Every log event of klog.Logger will be published to kafka. The logger will automatically fall back to stdout logger if kafka is unreachable.

This package implements all zerolog functionalities that are documented here: https://github.com/rs/zerolog

Basic Usage:

log := klog.Logger{
	Application:          "kafka-logging-test",
	Environment:          "development",
	KafkaBrokers:         []string{"kafka-bootstrap.svc.cluster.local:9094"},
	KafkaTopic:           "kafka-logging-test",
	KafkaProtocolVersion: "2.7.0",
}

// It will not error on kafka error. So every error here should terminate the program because there is a // problem with the stdout logger.

if err := log.Init(); err != nil {
	panic(err)
}

log.Info().

Msg("application started")

log.Warn().

Msg("an instance failed to start")

err := fmt.Errorf("hard moods in package")

log.Err(err).

Msg("package moods failed")

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSaramaConfigNil = errors.New("SaramaConfig is nil")
)

Functions

This section is empty.

Types

type KafkaWriter

type KafkaWriter interface {
	zerolog.LevelWriter
	io.Closer
}

KafkaWriter implements zerolog.LevelWriter interface

type Logger

type Logger struct {

	// Application keyword which is added to the logger as string field
	//
	// Example: "payment-gateway"
	Application string

	// Environment keyword which is added to the logger as string field
	//
	// Examples: local, development, staging, production
	Environment string

	// Brokers list of kafka brokers
	//
	// Example: kafka1:9094, kafka2:9094, kafka3:9094, kafka-bootstrap:9094
	KafkaBrokers []string

	// KafkaProtocolVersion to configure "github.com/Shopify/sarama"
	KafkaProtocolVersion string

	// KafkaTopic where to produce log messages.
	KafkaTopic string

	// SaramaConfig holds the configuration of Sarama.
	// It will be set by invoking Init if it's nil.
	//
	SaramaConfig *sarama.Config

	// AdditionalWriters that will be attached to the logger
	AdditionalWriters []zerolog.LevelWriter

	// Level sets the minimum logging level of zerolog
	//
	// Valid Options: TRACE, DEBUG, INFO, WARNING, ERROR
	Level string

	// Caller functions will be added as additional field to the global logger if true
	//
	// Note that With().Caller() can be added to every event of choice
	Caller bool
	// contains filtered or unexported fields
}

Logger holds the log instance

func (*Logger) Debug

func (l *Logger) Debug() *zerolog.Event

Debug starts a new message with debug level.

You must call Msg on the returned event in order to send the event.

func (*Logger) Err

func (l *Logger) Err(err error) *zerolog.Event

Err starts a new message with error level with err as a field if not nil or with info level if err is nil.

You must call Msg on the returned event in order to send the event.

func (*Logger) Error

func (l *Logger) Error() *zerolog.Event

Error starts a new message with error level.

You must call Msg on the returned event in order to send the event.

func (*Logger) Fatal

func (l *Logger) Fatal() *zerolog.Event

Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.

You must call Msg on the returned event in order to send the event.

func (*Logger) Info

func (l *Logger) Info() *zerolog.Event

Info starts a new message with info level.

You must call Msg on the returned event in order to send the event.

func (*Logger) Init

func (l *Logger) Init() error

Init will configure the Logger instance. If kafka producer can not be started the logger will fall back to stdout logger.

func (*Logger) Log

func (l *Logger) Log() *zerolog.Event

Log starts a new message with no level. Setting zerolog.GlobalLevel to zerolog.Disabled will still disable events produced by this method.

You must call Msg on the returned event in order to send the event.

func (*Logger) Panic

func (l *Logger) Panic() *zerolog.Event

Panic starts a new message with panic level. The message is also sent to the panic function.

You must call Msg on the returned event in order to send the event.

func (*Logger) Warn

func (l *Logger) Warn() *zerolog.Event

Warn starts a new message with warn level.

You must call Msg on the returned event in order to send the event.

Jump to

Keyboard shortcuts

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