logrusiowriter: github.com/cabify/logrusiowriter Index | Examples | Files

package logrusiowriter

import "github.com/cabify/logrusiowriter"

Index

Examples

Package Files

configs.go writer.go

Variables

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

func New Uses

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

Code:

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"

type Config Uses

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 Uses

type Configurer func(*writer)

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

func WithConfig Uses

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)

Code:

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 Uses

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

Code:

removeTimestampAndSetOutputToStdout(logrus.StandardLogger())

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

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

Output:

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

func WithFields Uses

func WithFields(fields logrus.Fields) Configurer

WithFields configures the fields with the ones provided

Code:

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 Uses

func WithLevel(lvl logrus.Level) Configurer

WithLevel configures the level with the one provided

Code:

removeTimestampAndSetOutputToStdout(logrus.StandardLogger())

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

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

Output:

level=error msg="Hello World!"

func WithLogger Uses

func WithLogger(logger logrus.FieldLogger) Configurer

WithLogger configures the logger with the one provided

Code:

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 Uses

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

Package logrusiowriter imports 2 packages (graph). Updated 2020-01-11. Refresh now. Tools for package owners.