logrusutil

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2018 License: MIT Imports: 5 Imported by: 1

README

Logrus Wrapper

Build Status codecov Go Report Card GoDoc

This project provides very basic wrappers for logrus including:

  • Log level parsing and handling.
  • Hooks for adding additional information to log messages.

Contributions are welcome but before opening a PR consider if your request would be better served as a contribution directly to the logrus project. This project was initially created so a few different projects could share the same code.

package main

import (
	"github.com/opalmer/logrusutil"
	"github.com/Sirupsen/logrus"
)

func main() {
	// Setup the root logger and hooks.
	if err := logrusutil.ConfigureLogger(logrus.StandardLogger(), logrusutil.NewConfig()); err != nil {
		panic(err)
	}
}

Case Sensitivity

The logrus project was renamed at one point from:

github.com/Sirupsen/logrus

To:

github.com/sirupsen/logrus

This causes conflicts to occur in certain cases if one or more of your dependencies are using the old import path. This project uses sirupsen which reflects what the majority of projects are using today.

Documentation

Index

Constants

View Source
const (
	// NoLevel and the following constants are used by MockFieldLogger to
	// indicate to MockFieldLogger.levelFunc what function is being called by
	// logrus.
	NoLevel = iota
	Debugf  // nolint
	Infof
	Printf
	Warnf
	Warningf
	Errorf
	Fatalf
	Panicf
	Debug
	Info
	Print
	Warn
	Warning
	Error
	Fatal
	Panic
	Debugln
	Infoln
	Println
	Warnln
	Warningln
	Errorln
	Fatalln
	Panicln

	// EmptyFormat is passed to MockFieldLogger.LevelFunc when there's
	// no format associated with a log entry.
	EmptyFormat = ""
)
View Source
const (
	// DisabledLevel may be passed into a *Config struct
	// to disable logging and discard output.
	DisabledLevel = "disabled"
)

Variables

View Source
var (
	// DefaultLevel is the default log level used in NewConfig()
	DefaultLevel = "warning"

	// DefaultHookLevel is the default level where hooks will
	// contribute log messages.
	DefaultHookLevel = "debug"

	// DefaultHookStackLevel is the level at which the hook will
	// apply.
	DefaultHookStackLevel = 4

	// DefaultCallerHookField sets the default field name that the
	// CallerHook hook will apply to a log entry.
	DefaultCallerHookField = "src"
)
View Source
var (
	// ErrLevelNotProvided is returned when a level was not provided
	// in the config struct.
	ErrLevelNotProvided = errors.New("level not provided")
)

Functions

func ConfigureLogger

func ConfigureLogger(logger *logrus.Logger, config *Config) error

ConfigureLogger will use the provided configuration to setup the root logrus logger.

func NewCallerHook

func NewCallerHook(disabled bool, depth int, field string, level logrus.Level) logrus.Hook

NewCallerHook constructs and returns logrus.Hook implementation based on the provided configuration.

Types

type CallerHook

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

CallerHook is a logrus hook which applies information about the caller to log messages. This is an implementation of the following interface:

https://github.com/sirupsen/logrus/blob/v1.0.3/hooks.go#L8

func (*CallerHook) Fire

func (c *CallerHook) Fire(entry *logrus.Entry) error

Fire will run the hook and apply it to the logrus entry.

func (*CallerHook) Levels

func (c *CallerHook) Levels() []logrus.Level

Levels returns the level(s) which this hook applies to.

type Config

type Config struct {
	Level           string
	HookLevel       string
	HookStackLevel  int
	CallerHookField string
}

Config is used to provided configuration information

func NewConfig

func NewConfig() *Config

NewConfig produces a *Config struct with reasonable defaults.

type Function added in v1.0.1

type Function uint32

Function is a type used by the constants below.

type MockFieldLogger added in v1.0.1

type MockFieldLogger struct {
	WithFieldFunc  func(string, interface{}) *logrus.Entry
	WithFieldsFunc func(logrus.Fields) *logrus.Entry
	WithErrorFunc  func(error) *logrus.Entry

	// LevelFunc is intended to provide a single implementation point
	// for all logging level both formatted and unformatted. You may wish
	// to review the implementation of this struct to understand how this
	// function is called in various cases.
	LevelFunc func(Function, string, ...interface{})
}

MockFieldLogger implements logrus.FieldLogger. This is intended for use in tests.

func (*MockFieldLogger) Debug added in v1.0.1

func (m *MockFieldLogger) Debug(args ...interface{})

Debug ...

func (*MockFieldLogger) Debugf added in v1.0.1

func (m *MockFieldLogger) Debugf(format string, args ...interface{})

Debugf ...

func (*MockFieldLogger) Debugln added in v1.0.1

func (m *MockFieldLogger) Debugln(args ...interface{})

Debugln ...

func (*MockFieldLogger) Error added in v1.0.1

func (m *MockFieldLogger) Error(args ...interface{})

Error ...

func (*MockFieldLogger) Errorf added in v1.0.1

func (m *MockFieldLogger) Errorf(format string, args ...interface{})

Errorf ...

func (*MockFieldLogger) Errorln added in v1.0.1

func (m *MockFieldLogger) Errorln(args ...interface{})

Errorln ...

func (*MockFieldLogger) Fatal added in v1.0.1

func (m *MockFieldLogger) Fatal(args ...interface{})

Fatal ...

func (*MockFieldLogger) Fatalf added in v1.0.1

func (m *MockFieldLogger) Fatalf(format string, args ...interface{})

Fatalf ...

func (*MockFieldLogger) Fatalln added in v1.0.1

func (m *MockFieldLogger) Fatalln(args ...interface{})

Fatalln ...

func (*MockFieldLogger) Info added in v1.0.1

func (m *MockFieldLogger) Info(args ...interface{})

Info ...

func (*MockFieldLogger) Infof added in v1.0.1

func (m *MockFieldLogger) Infof(format string, args ...interface{})

Infof ...

func (*MockFieldLogger) Infoln added in v1.0.1

func (m *MockFieldLogger) Infoln(args ...interface{})

Infoln ...

func (*MockFieldLogger) Panic added in v1.0.1

func (m *MockFieldLogger) Panic(args ...interface{})

Panic ...

func (*MockFieldLogger) Panicf added in v1.0.1

func (m *MockFieldLogger) Panicf(format string, args ...interface{})

Panicf ...

func (*MockFieldLogger) Panicln added in v1.0.1

func (m *MockFieldLogger) Panicln(args ...interface{})

Panicln ...

func (*MockFieldLogger) Print added in v1.0.1

func (m *MockFieldLogger) Print(args ...interface{})

Print ...

func (*MockFieldLogger) Printf added in v1.0.1

func (m *MockFieldLogger) Printf(format string, args ...interface{})

Printf ...

func (*MockFieldLogger) Println added in v1.0.1

func (m *MockFieldLogger) Println(args ...interface{})

Println ...

func (*MockFieldLogger) Warn added in v1.0.1

func (m *MockFieldLogger) Warn(args ...interface{})

Warn ...

func (*MockFieldLogger) Warnf added in v1.0.1

func (m *MockFieldLogger) Warnf(format string, args ...interface{})

Warnf ...

func (*MockFieldLogger) Warning added in v1.0.1

func (m *MockFieldLogger) Warning(args ...interface{})

Warning ...

func (*MockFieldLogger) Warningf added in v1.0.1

func (m *MockFieldLogger) Warningf(format string, args ...interface{})

Warningf ...

func (*MockFieldLogger) Warningln added in v1.0.1

func (m *MockFieldLogger) Warningln(args ...interface{})

Warningln ...

func (*MockFieldLogger) Warnln added in v1.0.1

func (m *MockFieldLogger) Warnln(args ...interface{})

Warnln ...

func (*MockFieldLogger) WithError added in v1.0.1

func (m *MockFieldLogger) WithError(err error) *logrus.Entry

WithError ...

func (*MockFieldLogger) WithField added in v1.0.1

func (m *MockFieldLogger) WithField(key string, value interface{}) *logrus.Entry

WithField ...

func (*MockFieldLogger) WithFields added in v1.0.1

func (m *MockFieldLogger) WithFields(fields logrus.Fields) *logrus.Entry

WithFields ...

Jump to

Keyboard shortcuts

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