elogrus

package module
v8.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 12 Imported by: 0

README

Elasticsearch Hook for Logrus

Intro

This is a hard fork of https://github.com/sohlich/elogrus. The original library used https://github.com/olivere/elastic, which is a non-official Elasticsearch go client. This fork uses the official client library.

Releases

This fork is designed to use the official Go client for Elasticsearch

Notice that the master branch always refers to the latest version of Elastic. If you want to use stable versions of elogus, you should use the packages released via gopkg.in.

Here's the version matrix:

Elasticsearch version Elastic Go Client version Package URL Remarks
7.x 7.0 gopkg.in/go-extras/elogrus.v7 Actively maintained.
8.x 8.0 gopkg.in/go-extras/elogrus.v8 Actively maintained.

For Elasticsearch 7.x

# We name v7 to align with elastic v7
go get github.com/elastic/go-elasticsearch/v7
go get gopkg.in/go-extras/elogrus.v7

For Elasticsearch 8.x

# We name v8 to align with elastic v8
go get github.com/elastic/go-elasticsearch/v8
go get gopkg.in/go-extras/elogrus.v8

Changelog

Usage

package main

import (
	"github.com/elastic/go-elasticsearch/v7"
	"github.com/sirupsen/logrus"
	"gopkg.in/go-extras/elogrus.v7"
)

func main() {
	log := logrus.New()
	client, err := elasticsearch.NewClient(elasticsearch.Config{
		Addresses: []string{"http://127.0.0.1:7777"},
	})
	if err != nil {
		log.Panic(err)
	}
	hook, err := elogrus.NewAsyncElasticHook(client, "localhost", logrus.DebugLevel, "mylog")
	if err != nil {
		log.Panic(err)
	}
	log.Hooks.Add(hook)
	log.WithFields(logrus.Fields{
		"name": "joe",
		"age":  42,
	}).Error("Hello world!")
}
Asynchronous hook
	...
	elogrus.NewAsyncElasticHook(client, "localhost", logrus.DebugLevel, "mylog")
	...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCannotCreateIndex Fired if the index is not created
	ErrCannotCreateIndex = fmt.Errorf("cannot create index")
)

Functions

This section is empty.

Types

type ElasticHook

type ElasticHook struct {

	// MessageModifierFunc is a function that can be called to create a
	// custom object to send to Elasticsearch for setting root fields
	// like "trace.id" or customizing other parts of the message
	MessageModifierFunc ModifyMessageFunc
	// contains filtered or unexported fields
}

ElasticHook is a logrus hook for ElasticSearch

func NewAsyncElasticHook

func NewAsyncElasticHook(client *elasticsearch.Client, host string, level logrus.Level, index string) (*ElasticHook, error)

NewAsyncElasticHook creates new hook with asynchronous log. client - ElasticSearch client with specific es version (v5/v6/v7/...) host - host of system level - log level index - name of the index in ElasticSearch

func NewAsyncElasticHookWithFunc

func NewAsyncElasticHookWithFunc(client *elasticsearch.Client, host string, level logrus.Level, indexFunc IndexNameFunc) (*ElasticHook, error)

NewAsyncElasticHookWithFunc creates new asynchronous hook with function that provides the index name. This is useful if the index name is somehow dynamic especially based on time. client - ElasticSearch client with specific es version (v5/v6/v7/...) host - host of system level - log level indexFunc - function providing the name of index

func NewBulkProcessorElasticHook

func NewBulkProcessorElasticHook(client *elasticsearch.Client, host string, level logrus.Level, index string) (*ElasticHook, error)

NewBulkProcessorElasticHook creates new hook that uses a bulk processor for indexing. client - ElasticSearch client with specific es version (v5/v6/v7/...) host - host of system level - log level index - name of the index in ElasticSearch

func NewBulkProcessorElasticHookWithFunc

func NewBulkProcessorElasticHookWithFunc(client *elasticsearch.Client, host string, level logrus.Level, indexFunc IndexNameFunc) (*ElasticHook, error)

NewBulkProcessorElasticHookWithFunc creates new hook with function that provides the index name. This is useful if the index name is somehow dynamic especially based on time that uses a bulk processor for indexing. client - ElasticSearch client with specific es version (v5/v6/v7/...) host - host of system level - log level indexFunc - function providing the name of index

func NewElasticHook

func NewElasticHook(client *elasticsearch.Client, host string, level logrus.Level, index string) (*ElasticHook, error)

NewElasticHook creates new hook. client - ElasticSearch client with specific es version (v5/v6/v7/...) host - host of system level - log level index - name of the index in ElasticSearch

func NewElasticHookWithFunc

func NewElasticHookWithFunc(client *elasticsearch.Client, host string, level logrus.Level, indexFunc IndexNameFunc) (*ElasticHook, error)

NewElasticHookWithFunc creates new hook with function that provides the index name. This is useful if the index name is somehow dynamic especially based on time. client - ElasticSearch client with specific es version (v5/v6/v7/...) host - host of system level - log level indexFunc - function providing the name of index

func (*ElasticHook) Cancel

func (hook *ElasticHook) Cancel()

Cancel all calls to elastic

func (*ElasticHook) Fire

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

Fire is required to implement Logrus hook

func (*ElasticHook) Levels

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

Levels Required for logrus hook implementation

type IndexNameFunc

type IndexNameFunc func() string

IndexNameFunc get index name

type Message

type Message struct {
	Host      string        `json:"host,omitempty"`
	Timestamp string        `json:"@timestamp"`
	File      string        `json:"file,omitempty"`
	Func      string        `json:"func,omitempty"`
	Message   string        `json:"message,omitempty"`
	Data      logrus.Fields `json:"data,omitempty"`
	Level     string        `json:"level,omitempty"`
}

type ModifyMessageFunc

type ModifyMessageFunc func(entry *logrus.Entry, message *Message) interface{}

ModifyMessageFunc is a function that can be used to generate the object sent to elasticsearch. The output value should be useable by json.Marshal

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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