logrus

package module
v0.0.0-...-1e5fe42 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2019 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 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{})

Debug implements the logger interface

func (*Logger) Error

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

Error implements the logger interface

func (*Logger) Fatal

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

Fatal implements the logger interface

func (*Logger) Info

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

Info implements the logger interface

func (*Logger) Warning

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

Warning implements the logger interface

Jump to

Keyboard shortcuts

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