logur

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2020 License: MIT Imports: 6 Imported by: 7

README

Logur error handler

GitHub Workflow Status Codecov Go Report Card Go Version go.dev reference

Error handler using Logur.

Installation

go get emperror.dev/handler/logur

Usage

package main

import (
	"github.com/goph/logur/adapters/logrusadapter"
	"github.com/sirupsen/logrus"

	logurhandler "emperror.dev/handler/logur"
)

func main() {
	logger := logrusadapter.New(logrus.New())
	handler := logurhandler.New(logger)
}

Development

When all coding and testing is done, please run the test suite:

make check

License

The MIT License (MIT). Please see License File for more information.

Documentation

Overview

Package logur provides an error handler using a Logur compatible logger.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorLogger

type ErrorLogger interface {
	// Error logs an Error event.
	//
	// Critical events that require immediate attention.
	Error(msg string, fields ...map[string]interface{})
}

ErrorLogger is a subset of the Logur Logger and LoggerContext interfaces used for error logging.

type Handler

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

Handler logs errors using a Logur compatible logger.

func New

func New(logger ErrorLogger) *Handler

New returns a new Handler.

Example
package main

import (
	"logur.dev/logur"

	logurhandler "emperror.dev/handler/logur"
)

func main() {
	logger := &logur.NoopLogger{}
	_ = logurhandler.New(logger)

}
Output:

func WithStackInfo

func WithStackInfo(handler *Handler) *Handler

WithStackInfo enables annotating every error passing through the handler with the function name and file line of the stack trace's top frame (if one is found).

Example

Maps are printed in key-sorted order as of Go 1.12 See https://golang.org/doc/go1.12#fmt

logger := newLogurLogger()
handler := logurhandler.WithStackInfo(logurhandler.New(logger))

err := errors.New("error")

handler.Handle(err)
Output:

error
map[file:example_go1_12_test.go:17 func:ExampleWithStackInfo]

func WithStackTrace added in v0.4.0

func WithStackTrace(handler *Handler) *Handler

WithStackTrace enables annotating every error passing through the handler with stack trace (if one is found).

func (*Handler) Handle

func (h *Handler) Handle(err error)

Handle records an error event and forwards it to the underlying logger.

Example
package main

import (
	"context"
	"fmt"

	"emperror.dev/errors"

	logurhandler "emperror.dev/handler/logur"
)

type errorLogger struct{}

func (e *errorLogger) Error(msg string, fields ...map[string]interface{}) {
	fmt.Println(msg)

	if len(fields) > 0 && len(fields[0]) > 0 {
		fmt.Println(fields[0])
	}
}

func (e *errorLogger) ErrorContext(ctx context.Context, msg string, fields ...map[string]interface{}) {
	fmt.Println(msg)

	if len(fields) > 0 && len(fields[0]) > 0 {
		fmt.Println(fields[0])
	}
}

func newLogurLogger() *errorLogger {
	return &errorLogger{}
}

func main() {
	logger := newLogurLogger()
	handler := logurhandler.New(logger)

	err := errors.New("error")

	handler.Handle(err)

}
Output:

error

func (*Handler) HandleContext added in v0.4.0

func (h *Handler) HandleContext(ctx context.Context, err error)

HandleContext records an error event and forwards it to the underlying logger.

Example
package main

import (
	"context"
	"fmt"

	"emperror.dev/errors"

	logurhandler "emperror.dev/handler/logur"
)

type errorLogger struct{}

func (e *errorLogger) Error(msg string, fields ...map[string]interface{}) {
	fmt.Println(msg)

	if len(fields) > 0 && len(fields[0]) > 0 {
		fmt.Println(fields[0])
	}
}

func (e *errorLogger) ErrorContext(ctx context.Context, msg string, fields ...map[string]interface{}) {
	fmt.Println(msg)

	if len(fields) > 0 && len(fields[0]) > 0 {
		fmt.Println(fields[0])
	}
}

func newLogurLogger() *errorLogger {
	return &errorLogger{}
}

func main() {
	logger := newLogurLogger()
	handler := logurhandler.New(logger)

	ctx := context.Background()
	err := errors.New("error")

	handler.HandleContext(ctx, err)

}
Output:

error

Jump to

Keyboard shortcuts

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