logrus

package module
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

krakend-logrus

A logrus logger for the KrakenD framework

Documentation

Overview

Package logrus provides a logger implementation based on the github.com/sirupsen/logrus pkg

Index

Examples

Constants

View Source
const Namespace = "github_com/devopsfaith/krakend-logrus"

Namespace is the key to look for extra configuration details

Variables

View Source
var ErrWrongConfig = errors.New("getting the extra config for the krakend-logrus module")

ErrWrongConfig is the error returned when there is no config under the namespace

Functions

func ConfigGetter

func ConfigGetter(e config.ExtraConfig) interface{}

ConfigGetter implements the config.ConfigGetter interface

Example
cfg := ConfigGetter(newExtraConfig("DEBUG")).(Config)
fmt.Println(cfg.JSONFormatter)
fmt.Printf("%+v\n", cfg.TextFormatter)
Output:

<nil>
&{ForceColors:false DisableColors:false DisableTimestamp:false FullTimestamp:true TimestampFormat: DisableSorting:false DisableLevelTruncation:false QuoteEmptyFields:false isTerminal:false FieldMap:map[] Once:{m:{state:0 sema:0} done:0}}

Types

type Config

type Config struct {
	Level         string                `json:"level"`
	StdOut        bool                  `json:"stdout"`
	Syslog        bool                  `json:"syslog"`
	Module        string                `json:"module"`
	TextFormatter *logrus.TextFormatter `json:"text"`
	JSONFormatter *logrus.JSONFormatter `json:"json"`
}

Config is the custom config struct containing the params for the logger

type Entry added in v0.0.3

type Entry struct {
	*logrus.Entry
}

An entry is the final or intermediate Logrus logging entry. It contains all the fields passed with WithField{,s}. It's finally logged when Trace, Debug, Info, Warn, Error, Fatal or Panic is called on it. These objects can be reused and passed around as much as you wish to avoid field duplication.

func (*Entry) Critical added in v0.0.6

func (e *Entry) Critical(v ...interface{})

func (*Entry) Debug added in v0.0.21

func (e *Entry) Debug(v ...interface{})

func (*Entry) Error added in v0.0.21

func (e *Entry) Error(v ...interface{})

func (*Entry) Fatal added in v0.0.23

func (e *Entry) Fatal(v ...interface{})

func (*Entry) Info added in v0.0.21

func (e *Entry) Info(v ...interface{})

func (*Entry) Warning added in v0.0.16

func (e *Entry) Warning(v ...interface{})

func (*Entry) WithField added in v0.0.13

func (entry *Entry) WithField(key string, value interface{}) *Entry

type Logger

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

Logger is a wrapper over a github.com/sirupsen/logrus logger

func NewLogger

func NewLogger(cfg config.ExtraConfig, ws ...io.Writer) (*Logger, error)

NewLogger returns a krakend logger wrapping a logrus logger

Example
cfg := map[string]interface{}{
	Namespace: Config{
		Level:  "DEBUG",
		Module: "pref",
		Syslog: false,
		StdOut: false,
		TextFormatter: &logrus.TextFormatter{
			DisableTimestamp: true,
		},
	},
}
buff := new(bytes.Buffer)
logger, err := NewLogger(cfg, buff)
if err != nil {
	fmt.Println(err.Error())
}

logger.Debug(debugMsg)
logger.Info(infoMsg)
logger.Warning(warningMsg)
logger.Error(errorMsg)
logger.Critical(criticalMsg)

fmt.Println(buff.String())
Output:

level=debug msg="Debug msg" module=pref
level=info msg="Info msg" module=pref
level=warning msg="Warning msg" module=pref
level=error msg="Error msg" module=pref
level=error msg="Critical msg" module=pref

func WrapLogger

func WrapLogger(l *logrus.Logger, module string) *Logger

WrapLogger wraps already configured logrus instance

Example
buff := new(bytes.Buffer)

logrusLogger := logrus.New()
logrusLogger.Formatter = &logrus.TextFormatter{DisableTimestamp: true}
logrusLogger.SetLevel(logrus.DebugLevel)
logrusLogger.SetOutput(buff)

logger := WrapLogger(logrusLogger, "pref")
logger.Debug(debugMsg)
logger.Info(infoMsg)
logger.Warning(warningMsg)
logger.Error(errorMsg)
logger.Critical(criticalMsg)

fmt.Println(buff.String())
Output:

level=debug msg="Debug msg" module=pref
level=info msg="Info msg" module=pref
level=warning msg="Warning msg" module=pref
level=error msg="Error msg" module=pref
level=error msg="Critical msg" module=pref

func (*Logger) Critical

func (l *Logger) Critical(v ...interface{})

Critical implements the logger interface but demotes to the error level

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

func (*Logger) NewEntry added in v0.0.12

func (l *Logger) NewEntry() *Entry

func (*Logger) Warning

func (l *Logger) Warning(v ...interface{})

func (*Logger) WithField added in v0.0.2

func (l *Logger) WithField(key string, value interface{}) *Entry

Jump to

Keyboard shortcuts

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