Skip to content
Hackable error handling package that uses stack-trace and static analysis to determine which func call causes the error. Pluggable to any logger in-place.
Branch: master
Clone or download
Latest commit 156d20a Apr 19, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples better regexp now Feb 22, 2019
.gitignore added gitignore Feb 20, 2019
LICENSE
README.md Added awesome badge Apr 19, 2019
defaults.go documentation, refactoring Feb 20, 2019
errlog.go better regexp now Feb 22, 2019
logger.go resolve gofmt -s Apr 5, 2019
regexp.go add comment to exported structs Apr 5, 2019
utils.go refactoring, comments, and documentation Feb 20, 2019

README.md

Go Report Card Documentation GitHub issues license Awesome

A simple package to enhance Go source code debugging

Example

Introduction

Use errlog to enhance your error logging with :

  • Code source highlight
  • Failing func recognition
  • Readable stack trace

Get started

Install

go get github.com/snwfdhmp/errlog

Import

import "github.com/snwfdhmp/errlog"

Usage

Now, replace some if err != nil with if errlog.Debug(err) to add debugging informations.

func someFunc() {
    //...
    if errlog.Debug(err) { // will debug & pass if err != nil, will ignore if err == nil
        return
    }
}

Configure like you need

You can configure your own logger with these options :

type Config struct {
    PrintFunc          func(format string, data ...interface{}) //Printer func (eg: fmt.Printf)
    LinesBefore        int  //How many lines to print *before* the error line when printing source code
    LinesAfter         int  //How many lines to print *after* the error line when printing source code
    PrintStack         bool //Shall we print stack trace ? yes/no
    PrintSource        bool //Shall we print source code along ? yes/no
    PrintError         bool //Shall we print the error of Debug(err) ? yes/no
    ExitOnDebugSuccess bool //Shall we os.Exit(1) after Debug has finished logging everything ? (doesn't happen when err is nil). Will soon be replaced by ExitFunc to enable panic-ing the current goroutine. (if you need this quick, please open an issue)
}

This definition may be outdated, visit the Config struct definition in godoc.org for the up to date definition

Example

We will use this sample program :

//someSmallFunc represents any func
func someSmallFunc() {
    fmt.Println("I do things !")
}

//someBigFunc represents any func having to handle errors from other funcs
func someBigFunc() {
    someSmallFunc()

    if err := someNastyFunc(); errlog.Debug(err) { //here, he want to catch an error
        return
    }

    someSmallFunc()
}

//someNastyFunc represents any failing func
func someNastyFunc() error {
    return errors.New("I'm failing for no reason")
}

func main() {
    fmt.Println("Start of the program")
    wrappingFunc()
    fmt.Println("End of the program")
}

func wrappingFunc() {
    someBigFunc()
}

Output

Console Output examples/basic.go

Example

Now let's see what we can do with a custom configuration.

debug := errlog.NewLogger(&errlog.Config{
    // PrintFunc is of type `func (format string, data ...interface{})`
    // so you can easily implement your own logger func.
    // In this example, logrus is used, but any other logger can be used.
    // Beware that you should add '\n' at the end of format string when printing.
    PrintFunc:          logrus.Printf,
    PrintSource:        true, //Print the failing source code
    LinesBefore:        2, //Print 2 lines before failing line
    LinesAfter:         1, //Print 1 line after failing line
    PrintError:         true, //Print the error
    PrintStack:         false, //Don't print the stack trace
    ExitOnDebugSuccess: true, //Exit if err
})

This definition may be outdated, visit the Config struct definition in godoc.org for the up to date definition

Output

Console Output examples/custom.go

Another Example

Errlog finds the exact line where the error is defined.

Output

Source Example: error earlier in the code

Documentation

Documentation can be found here : Documentation

Feedback

Feel free to open an issue for any feedback or suggestion.

I fix process issues quickly.

Contributions

PR are accepted as soon as they follow Golang common standards. For more information: https://golang.org/doc/effective_go.html

License information

license

Contributors

Project contribution

Minor fixes

You can’t perform that action at this time.