logtrace

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2022 License: MIT Imports: 5 Imported by: 2

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogtraceHook

type LogtraceHook struct {
	// The key (for the field in the logrus entry) to use for the stacktrace
	Key string
	// The log levels this hook should be fired for
	LogLevels []logrus.Level
	// Whether to capture a new stacktrace for errors without a stacktrace
	//
	// The captures tacktrace corresponds to the call of the log function, not the actual origin of the error
	// However, this may still help in identifying the path
	CaptureStackIfMissing bool
}

LogtraceHook is a logrus hook that attaches a stacktrace to log entries with errors

func DefaultLogtraceHook

func DefaultLogtraceHook() LogtraceHook

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

func (LogtraceHook) Fire

func (hook LogtraceHook) Fire(entry *logrus.Entry) error

Fire is called by logrus when to hook is supposed to run

func (LogtraceHook) Levels

func (hook LogtraceHook) Levels() []logrus.Level

type StackFrame

type StackFrame struct {
	// File is the path to the file containing the instruction of the StackFrame
	File string
	// The Line of the instruction of the StackFrame
	Line int
	// The Function of the instruction of the StackFrame
	Function string
	// The Package that contains the Function
	Package string
}

A StackFrame represents one frame in a stacktrace

Jump to

Keyboard shortcuts

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