errlog

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README ΒΆ

errlog

Test Go Reference Codecov Go Report Card

errlog is a error logging package based on log/slog standard library. It provides error logging with stack trace and source location. It does not require any third-party package.

πŸš€ Installation

go get github.com/ichizero/errlog

🧐 Usage

Initialize logger

errlog.NewHandler wraps slog.Handler, so you can provide *slog.JSONHandler, *slog.TextHandler, or any other handler.

h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})
hErr := errlog.NewHandler(h, &errlog.HandlerOptions{OverrideSource: true, SuppressStackTrace: false})
slog.SetDefault(slog.New(hErr))
Logging error with stack trace
With errlog.Err

errlog.Err wraps error with stack trace and returns slog.Attr with key error.

err := errors.New("test error")
slog.ErrorContext(ctx, "test", errlog.Err(err))
With custom error

errlog.NewHandler outputs stack trace with the error that implements errlog.StackTrace interface, so you can provide custom error with stack trace.

type yourCustomError struct {
	err error
	stack []uintptr
}

func (e yourCustomError) Stack() []uintptr {
	return e.stack
}

If so, you can log stack trace without using errlog.Err.

err := newYourCustomError("error")
slog.ErrorContext(ctx, "test", slog.Any("error", err))
Example usage
package main

import (
	"context"
	"errors"
	"log/slog"
	"os"

	"github.com/ichizero/errlog"
)

func main() {
	h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})
	hErr := errlog.NewHandler(h, &errlog.HandlerOptions{OverrideSource: true, SuppressStackTrace: false})
	slog.SetDefault(slog.New(hErr))

	ctx := context.Background()

	err := errors.New("test error")
	slog.ErrorContext(ctx, "test", errlog.Err(err))

	err = errlog.WrapError(err)
	slog.ErrorContext(ctx, "test", slog.Any("error", err))
}

Documentation ΒΆ

Overview ΒΆ

Example ΒΆ
package main

import (
	"context"
	"errors"
	"log/slog"
	"os"

	"github.com/ichizero/errlog"
)

func main() {
	h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{AddSource: true})
	hErr := errlog.NewHandler(h, &errlog.HandlerOptions{OverrideSource: true, SuppressStackTrace: false})
	slog.SetDefault(slog.New(hErr))

	ctx := context.Background()

	err := errors.New("test error")
	slog.ErrorContext(ctx, "test", errlog.Err(err))

	err = errlog.WrapError(err)
	slog.ErrorContext(ctx, "test", slog.Any("error", err))
}
Output:

Index ΒΆ

Examples ΒΆ

Constants ΒΆ

View Source
const (
	ErrorKey      = "error"
	StackTraceKey = "stack_trace"
)

Variables ΒΆ

This section is empty.

Functions ΒΆ

func Err ΒΆ

func Err(err error) slog.Attr

Err returns an attribute that contains the given error. If the error does not implement the StackTracer interface, it will be wrapped with the stack trace.

func WrapError ΒΆ

func WrapError(err error) error

WrapError wraps the given error with a stack trace.

Types ΒΆ

type Handler ΒΆ

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

Handler is a slog.Handler that adds error and stack trace information to log records.

func NewHandler ΒΆ

func NewHandler(base slog.Handler, opts *HandlerOptions) *Handler

NewHandler returns a new Handler that wraps the given base slog.handler.

func (*Handler) Enabled ΒΆ

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

Enabled is a thin wrapper around the base handler's Enabled method.

func (*Handler) Handle ΒΆ

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

Handle adds error and stack trace information to the log record.

func (*Handler) WithAttrs ΒΆ

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs is a thin wrapper around the base handler's WithAttrs method.

func (*Handler) WithGroup ΒΆ

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup is a thin wrapper around the base handler's WithGroup method.

type HandlerOptions ΒΆ

type HandlerOptions struct {
	// SuppressStackTrace suppresses the stack trace from being added to log records.
	SuppressStackTrace bool
	// OverrideSource overrides the source location of the log record with the source location of the error.
	OverrideSource bool
	// StackTraceFormatter is a function that formats the stack trace.
	StackTraceFormatter func(stack []uintptr) string
}

HandlerOptions contains options for the Handler.

type StackTracer ΒΆ

type StackTracer interface {
	Stack() []uintptr
}

StackTracer is an interface that represents an error that can provide a stack trace.

Jump to

Keyboard shortcuts

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