logan

package module
v3.8.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2022 License: MIT Imports: 6 Imported by: 1

README

Still alpha, some backward compatibility demages can happen.

Logan wraps logrus and adds:

  • Custom FError error with map for storing fields
  • WithStack to log stack of an error
  • WithRecover to log recover objects and retrieve stack from errors passed into panic

Synopsis:

    rootLog := logan.NewWithLevel(loganLogLevel).WithField("application", "appName")
    childLog := rootLog.WithField("service", "serviceName") // contains `application`
    clildLog.WithField("key", "value").WithError(err).WithStack(err).Error("Error happened.")

Fielded error usage example:

package main

import (
	"gitlab.com/distributed_lab/logan"
	"errors"
)

func main() {
	err := driveCar("Bob")
	if err != nil {
		log := logan.New()
		// Logan will log `car_color` here
		log.WithField("service", "car_manager").WithError(err).Error("Failed to start car.")
	}
}

func driveCar(driver string) error {
	var carColor string
	switch driver {
	case "John":
		// Only John drives blue car
		carColor = "BLUE"
	default:
		carColor = "RED"
	}

	err := startEngine(carColor)
	if err != nil {
	    // Mention `carColor` here, it is unknown from above.
		// Instead of logging the `carColor` here, put it into `err` as a field.
		return logan.Wrap(err, "Failed to start engine.").WithField("car_color", carColor)
	}

	return nil
}

// startEngine just returns simple error, if `carColor` is "RED".
func startEngine(carColor string) error {
	if carColor == "RED" {
	    // Do not add `carColor` into error here, it is known from above.
		return errors.New("Engine exploded.")
	}

	return nil
}

Documentation

Index

Constants

View Source
const NilValueToLog = "<nil>"

DEPRECATED: Use logan/v3 instead

Variables

View Source
var (
	ErrorKey = logrus.ErrorKey
	StackKey = "stack"
)

DEPRECATED: Use logan/v3 instead

Functions

func FromPanic

func FromPanic(rec interface{}) error

FromPanic extracts the err from the result of a recover() call.

DEPRECATED: Use logan/v3 instead

Types

type Entry

type Entry struct {
	*logrus.Entry
}

DEPRECATED: Use logan/v3 instead

func New

func New() *Entry

DEPRECATED: Use logan/v3 instead

func NewWithJSONFormatter

func NewWithJSONFormatter() (result *Entry)

DEPRECATED: Use logan/v3 instead

func NewWithLevel

func NewWithLevel(level Level) (result *Entry)

DEPRECATED: Use logan/v3 instead

func NewWithLevelFormatter

func NewWithLevelFormatter(level Level, formatter Formatter) (result *Entry)

DEPRECATED: Use logan/v3 instead

func NewWithLevelFormatterOut

func NewWithLevelFormatterOut(level Level, formatter Formatter, out io.Writer) (result *Entry)

DEPRECATED: Use logan/v3 instead

func NewWithLevelJSONFormatter

func NewWithLevelJSONFormatter(level Level) (result *Entry)

DEPRECATED: Use logan/v3 instead

func (*Entry) AddHook

func (e *Entry) AddHook(hook Hook)

DEPRECATED: Use logan/v3 instead

func (*Entry) AddLogrusHook

func (e *Entry) AddLogrusHook(hook logrus.Hook)

DEPRECATED: Use logan/v3 instead

func (*Entry) Debug

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

Debug logs a message at the debug severity.

DEPRECATED: Use logan/v3 instead

func (*Entry) Debugf

func (e *Entry) Debugf(format string, args ...interface{})

Debugf logs a message at the debug severity.

DEPRECATED: Use logan/v3 instead

func (*Entry) Error

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

Error logs a message at the Error severity.

DEPRECATED: Use logan/v3 instead

func (*Entry) Errorf

func (e *Entry) Errorf(format string, args ...interface{})

Errorf logs a message at the Error severity.

DEPRECATED: Use logan/v3 instead

func (*Entry) Info

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

Info logs a message at the Info severity.

DEPRECATED: Use logan/v3 instead

func (*Entry) Infof

func (e *Entry) Infof(format string, args ...interface{})

Infof logs a message at the Info severity.

DEPRECATED: Use logan/v3 instead

func (*Entry) Panic

func (e *Entry) Panic(args ...interface{})

Panic logs a message at the Panic severity.

DEPRECATED: Use logan/v3 instead

func (*Entry) Panicf

func (e *Entry) Panicf(format string, args ...interface{})

Panicf logs a message at the Panic severity.

DEPRECATED: Use logan/v3 instead

func (*Entry) SetLevel

func (e *Entry) SetLevel(level Level) *Entry

DEPRECATED: Use logan/v3 instead

func (*Entry) SetLoggerOut

func (e *Entry) SetLoggerOut(writer io.Writer) *Entry

DEPRECATED: Use logan/v3 instead

func (*Entry) Warn

func (e *Entry) Warn(args ...interface{})

Warn logs a message at the Warn severity.

DEPRECATED: Use logan/v3 instead

func (*Entry) Warnf

func (e *Entry) Warnf(format string, args ...interface{})

Warnf logs a message at the Warn severity.

DEPRECATED: Use logan/v3 instead

func (*Entry) WithError

func (e *Entry) WithError(err error) *Entry

DEPRECATED: Use logan/v3 instead

func (*Entry) WithField

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

DEPRECATED: Use logan/v3 instead

func (*Entry) WithFields

func (e *Entry) WithFields(fields F) *Entry

DEPRECATED: DEPRECATED: Use logan/v3 instead

func (*Entry) WithRecover

func (e *Entry) WithRecover(recoverData interface{}) *Entry

WithRecover creates `go-errors.Error` error from the `recoverData` and returns Entry with this error and its stack.

DEPRECATED: Use logan/v3 instead

func (*Entry) WithStack

func (e *Entry) WithStack(err error) *Entry

DEPRECATED: Use logan/v3 instead

type F

type F logrus.Fields

DEPRECATED: Use logan/v3 instead

type FError

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

DEPRECATED: Use logan/v3 instead

func (*FError) Cause

func (e *FError) Cause() error

DEPRECATED: Use logan/v3 instead

func (*FError) Error

func (e *FError) Error() string

DEPRECATED: Use logan/v3 instead

func (*FError) Fields

func (e *FError) Fields() F

DEPRECATED: Use logan/v3 instead

func (*FError) WithField

func (e *FError) WithField(key string, value interface{}) FieldedErrorI

WithField returns the same instance

DEPRECATED: Use logan/v3 instead

func (*FError) WithFields

func (e *FError) WithFields(fields F) FieldedErrorI

WithFields returns the same instance

DEPRECATED: Use logan/v3 instead

type FieldedEntityI

type FieldedEntityI interface {
	GetLogFields() F
}

DEPRECATED: Use logan/v3 instead

type FieldedErrorI

type FieldedErrorI interface {
	// DEPRECATED
	Error() string
	// DEPRECATED
	Fields() F
	// DEPRECATED
	WithField(key string, value interface{}) FieldedErrorI
	// DEPRECATED
	WithFields(fields F) FieldedErrorI
}

DEPRECATED: Use logan/v3 instead

func NewError

func NewError(msg string) FieldedErrorI

DEPRECATED: Use logan/v3 instead

func Wrap

func Wrap(base error, msg string) FieldedErrorI

If base is nil, Wrap returns nil.

DEPRECATED: Use logan/v3 instead

type Formatter

type Formatter logrus.Formatter

DEPRECATED: Use logan/v3 instead

type Hook

type Hook interface {
	Levels() []Level
	Fire(*Entry) error
}

DEPRECATED: Use logan/v3 instead

type Level

type Level logrus.Level

DEPRECATED: Use logan/v3 instead

const (

	// DEPRECATED
	PanicLevel Level = iota
	// DEPRECATED
	FatalLevel
	// DEPRECATED
	ErrorLevel
	// DEPRECATED
	WarnLevel
	// DEPRECATED
	InfoLevel
	// DEPRECATED
	DebugLevel
)

func ParseLevel

func ParseLevel(level string) (Level, error)

DEPRECATED: Use logan/v3 instead

type Stackable

type Stackable interface {
	Stack() []byte
}

DEPRECATED: Use logan/v3 instead

Directories

Path Synopsis
v2
v3

Jump to

Keyboard shortcuts

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