formatter

package module
v0.0.0-...-070a703 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2019 License: MIT Imports: 3 Imported by: 0

README

logrus-module-formatter

This package is intended to be used as logrus formatter.

You can use this as a wrapper over any other logrus formatter with the added benefit of allowing you to filter what actually gets printed by so-called "modules".

A module is whatever you want it to be. Just specify it via fields when logging something logrus.WithField("module", "my-special-module").Info("<3") and it will be considered as such.

Ok, ok, too many words ... here's an example:

  • for any kind of log that belongs to the module very-bugged-module-that-needs-a-lot-of-debugging, I want to see logs >= logrus.TraceLevel while for all other modules I only want to see logs >= logrus.ErrorLevel.

Installation

This package supports go modules:

go get github.com/kwix/logrus-module-formatter

Configuration

The ModuleFormatter accepts a ModulesMap as configuration.

ModulesMap is in the form of map[string]logrus.Level. Here's an example:

ModulesMap{
    "*":     logrus.WarnLevel,
    "test":  logrus.InfoLevel,
    "test1": logrus.DebugLevel,
}

Notes:

  • you should use "*": [level] to specify the global logging level; if this doesn't exist, everything that's not in the whitelist will be ingored
    • this formatter automatically calls logrus.SetLevel(logrus.TraceLevel) as a workaround to the fact that a log wouldn't reach the formatter if the level is not >= logrus global level

Usage

import (
	modules "github.com/kwix/logrus-module-formatter"
	"github.com/sirupsen/logrus"
)

f, err := modules.New(modules.ModulesMap{
    "*":     logrus.WarnLevel,
    "test":  logrus.InfoLevel,
    "test1": logrus.DebugLevel,
})
if err != nil {
    panic(err)
}

logrus.SetFormatter(f)

logrus.WithField("module", "test").Debug("This should be ignored")
logrus.WithField("module", "test1").Info("This should be displayed")
logrus.WithField("module", "test2").Warn("This should be displayed too")

Output:

time="2019-06-04T21:46:44+03:00" level=info msg="This should be displayed"
time="2019-06-04T21:46:44+03:00" level=warning msg="This should be displayed too"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ModuleFormatter

type ModuleFormatter struct {
	// contains filtered or unexported fields
}

func New

func New(modules ModulesMap) (*ModuleFormatter, error)

func NewWithFormatter

func NewWithFormatter(modules ModulesMap, formatter logrus.Formatter) (*ModuleFormatter, error)

func (*ModuleFormatter) Format

func (f *ModuleFormatter) Format(entry *logrus.Entry) ([]byte, error)

type ModulesMap

type ModulesMap map[string]logrus.Level

func NewModulesMap

func NewModulesMap(logging string) ModulesMap

Jump to

Keyboard shortcuts

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