logrus_fluent

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2018 License: Apache-2.0 Imports: 6 Imported by: 56

README

Fluentd Hook for Logrus

GoDoc Release Travis Status wercker Status Coveralls Coverage Go Report Card Downloads

Usage

import (
	"github.com/sirupsen/logrus"
	"github.com/evalphobia/logrus_fluent"
)

func main() {
	hook, err := logrus_fluent.NewWithConfig(logrus_fluent.Config{
		Host: "localhost",
		Port: 24224,
	})
	if err != nil {
		panic(err)
	}

	// set custom fire level
	hook.SetLevels([]logrus.Level{
		logrus.PanicLevel,
		logrus.ErrorLevel,
	})

	// set static tag
	hook.SetTag("original.tag")

	// ignore field
	hook.AddIgnore("context")

	// filter func
	hook.AddFilter("error", logrus_fluent.FilterError)

	logrus.AddHook(hook)
}

func logging(ctx context.Context) {
	logrus.WithFields(logrus.Fields{
		"value":   "some content...",
		"error":   errors.New("unknown error"), // this field will be applied filter function in the hook.
		"context": ctx,                         // this field will be ignored in the hook.
	}).Error("error message")
}

Special fields

Some logrus fields have a special meaning in this hook.

  • tag is used as a fluentd tag. (if tag is omitted, Entry.Message is used as a fluentd tag, unless a static tag is set for the hook with hook.SetTag)

Documentation

Index

Constants

View Source
const (
	// TagName is struct field tag name.
	// Some basic option is allowed in the field tag,
	//
	// type myStruct {
	//     Value1: `fluent:"value_1"`    // change field name.
	//     Value2: `fluent:"-"`          // always omit this field.
	//     Value3: `fluent:",omitempty"` // omit this field when zero-value.
	// }
	TagName = "fluent"
	// TagField is logrus field name used as fluentd tag
	TagField = "tag"
	// MessageField is logrus field name used as message.
	// If missing in the log fields, entry.Message is set to this field.
	MessageField = "message"
)

Variables

This section is empty.

Functions

func ConvertToValue

func ConvertToValue(p interface{}, tagName string) interface{}

ConvertToValue make map data from struct and tags

func FilterError

func FilterError(v interface{}) interface{}

FilterError is a filter function to convert error type to string type.

Types

type Config added in v0.4.0

type Config struct {
	Port                  int
	Host                  string
	LogLevels             []logrus.Level
	DisableConnectionPool bool // Fluent client will be created every logging if true.
	DefaultTag            string
	DefaultMessageField   string
	DefaultIgnoreFields   map[string]struct{}
	DefaultFilters        map[string]func(interface{}) interface{}

	// from fluent.Config
	// see https://github.com/fluent/fluent-logger-golang/blob/master/fluent/fluent.go
	FluentNetwork      string
	FluentSocketPath   string
	Timeout            time.Duration
	WriteTimeout       time.Duration
	BufferLimit        int
	RetryWait          int
	MaxRetry           int
	TagPrefix          string
	AsyncConnect       bool
	MarshalAsJSON      bool
	SubSecondPrecision bool
}

Config is settings for FluentHook.

func (Config) FluentConfig added in v0.4.0

func (c Config) FluentConfig() fluent.Config

FluentConfig converts data to fluent.Config.

type FluentHook

type FluentHook struct {
	// Fluent is actual fluentd logger.
	// If set, this logger is used for logging.
	// otherwise new logger is created every time.
	Fluent *fluent.Fluent
	// contains filtered or unexported fields
}

FluentHook is logrus hook for fluentd.

func New

func New(host string, port int) (*FluentHook, error)

New returns initialized logrus hook for fluentd with persistent fluentd logger.

func NewHook

func NewHook(host string, port int) *FluentHook

NewHook returns initialized logrus hook for fluentd. (** deperecated: use New() or NewWithConfig() **)

func NewWithConfig added in v0.4.0

func NewWithConfig(conf Config) (*FluentHook, error)

NewWithConfig returns initialized logrus hook by config setting.

func (*FluentHook) AddFilter

func (hook *FluentHook) AddFilter(name string, fn func(interface{}) interface{})

AddFilter adds a custom filter function.

func (*FluentHook) AddIgnore

func (hook *FluentHook) AddIgnore(name string)

AddIgnore adds field name to ignore.

func (*FluentHook) Fire

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

Fire is invoked by logrus and sends log to fluentd logger.

func (*FluentHook) Levels

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

Levels returns logging level to fire this hook.

func (*FluentHook) SetLevels

func (hook *FluentHook) SetLevels(levels []logrus.Level)

SetLevels sets logging level to fire this hook.

func (*FluentHook) SetMessageField added in v0.3.1

func (hook *FluentHook) SetMessageField(messageField string)

SetMessageField sets custom message field.

func (*FluentHook) SetTag

func (hook *FluentHook) SetTag(tag string)

SetTag sets custom static tag to override tag in the message fields.

func (*FluentHook) Tag

func (hook *FluentHook) Tag() string

Tag returns custom static tag.

Jump to

Keyboard shortcuts

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