GithubHelp home page GithubHelp logo

xuanyingcool / kafkalogrus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tracer0tong/kafkalogrus

0.0 0.0 0.0 14 KB

Modified version of Kafka hook for Logrus

License: Apache License 2.0

Go 100.00%

kafkalogrus's Introduction

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

kafkalogrus's People

Contributors

sebito91 avatar thomasxnguy avatar tracer0tong avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.