logrusredis

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2020 License: MIT Imports: 8 Imported by: 0

README

logrusredis hook

a logrus hook print log to redis list(RPUSH command).

Background

logrus is my common structure log package for Go. It print log to standard output or files(by hook), so I can collect log through filebeat/journalbeat to ELK.

But in some scenario:

  • ELK for me is too heavyweight. for example, grafana/loki is more suitable for me;
  • I want file special logs for data statistics, like logrus.Fields marked {"logid": "xxx"};

So, one side filter log and insert log to message queue, the other side, consume log from message queue, insert to MongoDB/MySQL/Loki/...

Redis list can be used as a simple message queue by RPUSH LPOP. Other message queue tool (Kafka) can also as one choose, but I like redis(it simple deploy, simple use).

Install

go get github.com/zhangjie2012/logrusredis-hook

Usage

  option := RedisOption{
	  Addr:     "localhost:6379",
	  Password: "",
	  DB:       0,
	  Key:      "logrusredis.hook",
  }
  hook, _ := NewHook(appName, &option, nil)
  logrus.AddHook(hook)

Sometime, We want customize inserted redis struct, you can customize a LogWashFunc, checkout logrusredis_test.go file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultLogWashFunc

func DefaultLogWashFunc(appName string, t time.Time, metadata logrus.Fields, caller *runtime.Frame, level logrus.Level, message string) []byte

DefaultLogWashFunc log filter and serialization

  • use msgpack serialize log
  • level > 'DEBUG'
  • set current instance ip

Types

type DefaultLogS

type DefaultLogS struct {
	Timestamp int64         `msgpack:"@timestamp"`
	MetaData  logrus.Fields `msgpack:"@metadata"` // map[string]interface{}
	Ip        string        `msgpack:"@ip"`
	Level     string        `msgpack:"@level"`
	Caller    string        `msgpack:"@caller"`
	Message   string        `msgpack:"@message"`
}

type LogWashFunc

type LogWashFunc func(appName string, t time.Time, metadata logrus.Fields, caller *runtime.Frame, level logrus.Level, message string) []byte

type LogrusRedisHook

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

func NewHook

func NewHook(appName string, option *RedisOption, logWashFunc LogWashFunc) (*LogrusRedisHook, error)

func (*LogrusRedisHook) Fire

func (h *LogrusRedisHook) Fire(e *logrus.Entry) (err error)

func (*LogrusRedisHook) Levels

func (h *LogrusRedisHook) Levels() []logrus.Level

type RedisOption

type RedisOption struct {
	Addr     string
	Password string
	DB       int
	Key      string
}

Jump to

Keyboard shortcuts

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