logan

package
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: 0

README

Still alpha, some backward compatibility demages can happen.

Logan wraps logrus and adds:

  • Custom 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.New().Level(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/v2"
	"gitlab.com/distributed_lab/logan/v2/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 errors.WithField("car_color", carColor).Wrap(err, "Failed to start engine.")
	}

	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

This section is empty.

Variables

View Source
var (
	// DEPRECATED
	// Use logan/v3 instead
	ErrorKey = logrus.ErrorKey
	// DEPRECATED
	// Use logan/v3 instead
	StackKey = "stack"
)

DEPRECATED: Use logan/v3 instead

Functions

This section is empty.

Types

type Entry

type Entry struct {
	*logrus.Entry
}

DEPRECATED: Use logan/v3 instead

func New

func New() *Entry

DEPRECATED: Use Entry from 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) Formatter

func (e *Entry) Formatter(formatter Formatter) *Entry

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) Level

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

DEPRECATED: Use logan/v3 instead

func (*Entry) Out

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

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) 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: 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 map[string]interface{}

F type is for logging fields, used to pass to `WithFields`.

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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