Home

Awesome

Errlog: reduce debugging time while programming Go Report Card Awesome Documentation GitHub issues license

Example

<a href="https://www.buymeacoffee.com/snwfdhmp" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 30px !important;width: 120px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>

Introduction

Use errlog to improve error logging and speed up debugging while you create amazing code :

Go to
Get started
Documentation
Examples
Tweaking
Feedbacks
Contributions
License
Contributors

Get started

Install

go get github.com/snwfdhmp/errlog

Usage

Replace your 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
    }
}

In production, call errlog.DefaultLogger.Disable(true) to enable no-op (equivalent to if err != nil)

Tweak as you need

You can configure your own logger with the following 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)
}

As we don't yet update automatically this README immediately when we add new features, this definition may be outdated. (Last update: 2019/08/07) See the struct definition in godoc.org for the up to date definition

Example

Try yourself

Name and link Description 
Basicstandard usage, quick setup
Customguided configuration for fulfilling your needs
Disabledhow to disable the logging & debugging (eg: for production use)
Failing line far awayexample of finding the func call that caused the error while it is lines away from the errlog.Debug call
Pretty stack tracepretty stack trace printing instead of debugging.

Just read

Basic example

Note that in the example, you will see some unuseful func. Those are made to generate additional stack trace levels for the sake of example

We're going to use this sample program :

func main() {
    fmt.Println("Program start")

    wrapingFunc() //call to our important function

    fmt.Println("Program end")
}

func wrapingFunc() {
    someBigFunction() // call some func 
}

func someBigFunction() {
    someDumbFunction() // just random calls
    someSmallFunction() // just random calls
    someDumbFunction() // just random calls

    // Here it can fail, so instead of `if err  != nil` we use `errlog.Debug(err)`
    if err := someNastyFunction(); errlog.Debug(err) {
        return
    }

    someSmallFunction() // just random calls
    someDumbFunction() // just random calls
}

func someSmallFunction() {
    _ = fmt.Sprintf("I do things !")
}

func someNastyFunction() error {
    return errors.New("I'm failing for some reason") // simulate an error
}

func someDumbFunction() bool {
    return false // just random things
}

Output

Console Output examples/basic.go

We are able to detect and point out which line is causing the error.

Custom Configuration Example

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
})

Please note: This definition may be outdated. (Last update: 2019/08/07) See the struct definition in godoc.org for the up to date definition

Output

Console Output examples/custom.go

When the failing func call is a few lines away

Even when the func call is a few lines away, there is no problem for finding it.

Output

Source Example: error earlier in the code

Documentation

Documentation can be found here : Documentation

Feedbacks

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

I fix process issues quickly.

Contributions

We are happy to collaborate with you :

When submitting a PR, please apply Effective Go best practices. For more information: https://golang.org/doc/effective_go.html

License information

Click the following badge to open LICENSE information.

license

Contributors

Major

Minor fixes