Use errlog to improve error logging and speed up debugging while you create amazing code :
- Highlight source code
- Detect and point out which func call is causing the fail
- Pretty stack trace
- No-op mode for production
- Easy implementation, adaptable logger
- Plug to any current project without changing you or your teammates habits
- Plug to your current logging system
Go to |
---|
Get started |
Documentation |
Examples |
Tweaking |
Feedbacks |
Contributions |
License |
Contributors |
go get github.com/snwfdhmp/errlog
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
)
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
Name and link | Description |
---|---|
Basic | standard usage, quick setup |
Custom | guided configuration for fulfilling your needs |
Disabled | how to disable the logging & debugging (eg: for production use) |
Failing line far away | example of finding the func call that caused the error while it is lines away from the errlog.Debug call |
Pretty stack trace | pretty stack trace printing instead of debugging. |
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
}
We are able to detect and point out which line is causing the error.
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
Even when the func call is a few lines away, there is no problem for finding it.
Documentation can be found here :
Feel free to open an issue for any feedback or suggestion.
I fix process issues quickly.
We are happy to collaborate with you :
- Ask for a new feature: Open an issue
- Add your feature: Open a PR
When submitting a PR, please apply Effective Go best practices. For more information: https://golang.org/doc/effective_go.html
Click the following badge to open LICENSE information.