Categorygithub.com/neumantm/logtrace
modulepackage
1.0.0
Repository: https://github.com/neumantm/logtrace.git
Documentation: pkg.go.dev

# README

logtrace

Go Reference CI

Logrus hook for attaching an error traceback to a log entry

This project is a hook for the golang logging libary logrus. It attaches a stacktrace field to log entries with an error. It is primarily developed to work with the error libary merry/v2. However, it also works with all errors implementing a Callers() function like for example the errors from go-errors.

Usage

To use the hook, just register it with logrus:

import "github.com/neumantm/logtrace"

func setupLogging() {
    // ...
    logrus.AddHook(logtrace.DefaultLogtraceHook())
    // Alternatively with custom parameters:
    //logrus.AddHook(logtrace.LogtraceHook{
    //    Key:                   "SomeKey",
    //    LogLevels:             logrus.AllLevels,
    //    CaptureStackIfMissing: false,
    //})
    // ...
}

The default hook runs on all levels uses the key stacktrace and captures the stack if the error does not have a compatible stack.

Stacktrace accuracy

Sometimes the entries on the stacktrace may not be what you expect because of compiler optimizations. To avoid this problem (during development or debugging) disable compiler optimizations and inlining by passing -gcflags '-N -l' to go build or go run.

# Functions

DefaultLogtraceHook returns the default LogtraceHook It has the following properties - The key is "stacktrace" - The hook runs on all log levels - For errors without a stacktrace a new stacktrace is captured corresponding to the call of the log function.

# Structs

LogtraceHook is a logrus hook that attaches a stacktrace to log entries with errors.
A StackFrame represents one frame in a stacktrace.