logrusiowriter

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2019 License: Apache-2.0 Imports: 2 Imported by: 0

README

logrusiowriter

io.Writer implementation using logrus

Travis CI build status GoDoc

Motivation

Many golang libraries use the golang's log package to print their logs. This means that if your application uses logrus to print structured logging, those packages will print a format that is (probably) incompatible with yours, and you may end losing logs in your logs collector because they can't be parsed properly.

Solution

Print the logs written using log.Printf through logrus, by setting log.SetOutput to an io.Writer implementation that uses logrus as output, i.e.:

	log.SetOutput(logrusiowriter.New())

See example_*_test.go files to find testable examples that serve as documentation.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var OnLevelParseError = func(err error) logrus.Level {
	logrus.Errorf("Can't parse level: %s", err)
	return logrus.InfoLevel
}

OnLevelParseError will be invoked if logrus is unable to parse the string level provided in the configuration The default behavior is to log it with logrus and return a default Info level, you can change this to log in some other system or to panic Changing this is not thread safe, so it might be a good idea to change it in a init() function

Functions

func New

func New(cfg ...Configurer) io.Writer

New creates a new io.Writer, and configures it with the provided configurers Provided Configurers overwrite previous values as they're applied If no Configurers provided, the writer will log with Info level and no fields using the logrus.StandardLogger

Example
removeTimestampAndSetOutputToStdout(logrus.StandardLogger())

log.SetOutput(logrusiowriter.New())
log.SetFlags(0) // no date on standard logger

log.Printf("Standard log")
Output:

level=info msg="Standard log"

Types

type Config

type Config struct {
	Level                   string        `default:"info"`
	Fields                  logrus.Fields `default:"logger:stdlib"`
	TrailingNewLineTrimming bool          `default:"true"`
}

Config holds the configuration to be used with WithConfig() configurer This struct is useful to embed into configuration structs parsed with libraries like envconfig

type Configurer

type Configurer func(*writer)

Configurer configures the writer, use one of the With* functions to obtain one

func WithConfig

func WithConfig(cfg Config) Configurer

WithConfig creates a configurer from the configuration provided as a struct If it's unable to parse the Level provided as a string, it will invoke the OnLevelParseError function and set the level returned by that function (a default value)

Example
removeTimestampAndSetOutputToStdout(logrus.StandardLogger())

config := logrusiowriter.Config{
	Level: "warning",
	Fields: map[string]interface{}{
		"config": "struct",
	},
}

writer := logrusiowriter.New(
	logrusiowriter.WithConfig(config),
)

_, _ = fmt.Fprint(writer, "Hello World!")
Output:

level=warning msg="Hello World!" config=struct

func WithConfigInterface

func WithConfigInterface(cfg interface {
	Level() logrus.Level
	Fields() logrus.Fields
	Logger() logrus.FieldLogger
}) Configurer

WithConfigInterface creates a configurer from the configuration provided as an interface

Example
removeTimestampAndSetOutputToStdout(logrus.StandardLogger())

writer := logrusiowriter.New(
	logrusiowriter.WithConfigInterface(configProvider{}),
)

_, _ = fmt.Fprint(writer, "Hello World!")
Output:

level=trace msg="Hello World!" config=interface

func WithFields

func WithFields(fields logrus.Fields) Configurer

WithFields configures the fields with the ones provided

Example
removeTimestampAndSetOutputToStdout(logrus.StandardLogger())

writer := logrusiowriter.New(
	logrusiowriter.WithFields(logrus.Fields{
		"config": "fields",
		"other":  288,
	}),
)

_, _ = fmt.Fprint(writer, "Hello World!")
Output:

level=info msg="Hello World!" config=fields other=288

func WithLevel

func WithLevel(lvl logrus.Level) Configurer

WithLevel configures the level with the one provided

Example
removeTimestampAndSetOutputToStdout(logrus.StandardLogger())

writer := logrusiowriter.New(
	logrusiowriter.WithLevel(logrus.ErrorLevel),
)

_, _ = fmt.Fprint(writer, "Hello World!")
Output:

level=error msg="Hello World!"

func WithLogger

func WithLogger(logger logrus.FieldLogger) Configurer

WithLogger configures the logger with the one provided

Example
logger := logrus.New()
removeTimestampAndSetOutputToStdout(logger)
logger.SetLevel(logrus.TraceLevel)

writer := logrusiowriter.New(
	logrusiowriter.WithLogger(logger),
)

_, _ = fmt.Fprint(writer, "Hello World!")
Output:

level=info msg="Hello World!"

func WithTrailingNewLineTrimming added in v1.1.0

func WithTrailingNewLineTrimming(trim bool) Configurer

WithTrailingNewLineTrimming configures trailing newline trimming. This is true by default, because log.Print adds a newline in the end of its messages, which does not make sense inside of a logrus log

Jump to

Keyboard shortcuts

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