elogrus

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: MIT Imports: 7 Imported by: 0

README

ElasticSearch Hook for Logrus

Releases

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 version Package URL Remarks
7.x 7.0 gopkg.in/sohlich/elogrus.v7 Use Go modules.
6.x 6.0 gopkg.in/sohlich/elogrus.v3 Actively maintained.
5.x 5.0 gopkg.in/sohlich/elogrus.v2 Actively maintained.
2.x 3.0 gopkg.in/sohlich/elogrus.v1 Deprecated. Please update.

For elastic search 7.x

# We name v7 to align with elastic v7
go get gopkg.in/sohlich/elogrus.v7
go get github.com/olivere/elastic/v7

For elastic search 6.x

go get gopkg.in/sohlich/elogrus.v3
go get github.com/olivere/elastic

For elastic search 5.x

go get gopkg.in/sohlich/elogrus.v2
go get gopkg.in/olivere/elastic.v5

Changelog

  • elastic 7.x support (currently in master)

Usage

package main

import (
	"github.com/olivere/elastic/v7"
	"github.com/sirupsen/logrus"
	"gopkg.in/sohlich/elogrus.v7"
)

func main() {
	log := logrus.New()
	client, err := elastic.NewClient(elastic.SetURL("http://localhost:9200"))
	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 {
	// contains filtered or unexported fields
}

ElasticHook is a logrus hook for ElasticSearch

func NewAsyncElasticHook

func NewAsyncElasticHook(client *elastic.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 *elastic.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 *elastic.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 *elastic.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 *elastic.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 *elastic.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 {
	logrus.Fields
	Host      string `json:host`
	Timestamp string `json:"@timestamp"`
	Message   string `json:message`
	Level     string `json:level`
}

type MessageTransformer

type MessageTransformer func(Message)

MessageTransformer transforms the content that is sent to elasticsearch

Jump to

Keyboard shortcuts

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