logerr

package module
v0.0.0-...-5e4972c Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2018 License: MIT Imports: 0 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

logerr

Annotate errors with log fields. logerr is intended to bridge the gap between logrus.WithFields and error wrapping libraries.

Example

package main

import (
	"github.com/JohannWeging/logerr"
	"github.com/juju/errors"
	log "github.com/sirupsen/logrus"
)

func main() {
	err := job()
	if err != nil {
		fields := logerr.GetFields(err)
		log.WithFields(fields).Errorf("job failed: %s", err)
	}
}

func job() error {
	err := task()
	if err != nil {
		err = logerr.WithField(err, "jobID", "0")
		err = errors.Annotate(err, "task failed")
		return err
	}
	return nil
}

func task() error {
	return errors.New("cause")
}

ERRO[0000] job failed: task failed: cause                jobID=0

Implemented Interfaces

logerr.Error implements 2 interfaces:


// github.com/juju/errors
type wrapper interface {
	Underlying() error
}

// github.com/pkg/errors
type causer interface {
	Cause() error
}

Documentation

Overview

Example
package main

import (
	"os"

	"github.com/JohannWeging/logerr"
	"github.com/juju/errors"
	log "github.com/sirupsen/logrus"
)

func main() {
	// make logrus play nice with go test
	log.SetOutput(os.Stdout)
	log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})

	err := job()
	if err != nil {
		fields := logerr.GetFields(err)
		log.WithFields(fields).Errorf("job failed: %s", err)
	}
}

func job() error {
	err := task()
	if err != nil {
		err = logerr.WithField(err, "jobID", "0")
		err = errors.Annotate(err, "task failed")
		return err
	}
	return nil
}

func task() error {
	return errors.New("cause")
}
Output:
level=error msg="job failed: task failed: cause" jobID=0

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeferWithFields

func DeferWithFields(err *error, fields map[string]interface{})

func GetFields

func GetFields(err error) map[string]interface{}

GetFields returns the log fields from the wrapped errors

func WithField

func WithField(err error, key string, value interface{}) error

WithField adds a single field to the error

func WithFields

func WithFields(err error, fields map[string]interface{}) error

WithFields adds a new field map to the error

Types

type Error

type Error struct {
	Fields map[string]interface{}
	// contains filtered or unexported fields
}

Error holds the added fields

func (*Error) Cause

func (e *Error) Cause() error

Cause implements the pkg/errors causer interface

func (*Error) Error

func (e *Error) Error() string

Error returns the error message of the wrapped errors

func (*Error) Underlying

func (e *Error) Underlying() error

Underlying implements the juju/errors wrapper interface

type Fields

type Fields map[string]interface{}

Fields are the log fields

Jump to

Keyboard shortcuts

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