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 ΒΆ
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.
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.
Click to show internal directories.
Click to hide internal directories.