Coder Social home page Coder Social logo

imskyer / errlog Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snwfdhmp/errlog

0.0 2.0 0.0 59 KB

Enhance Golang source code debugging with code highlight, failing func call recognition and pretty stack trace printing.

License: MIT License

Go 100.00%

errlog's Introduction

Go Report Card Documentation GitHub issues license

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

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

Example

We will use this sample program :

package main

import (
	"errors"
	"fmt"

	"github.com/snwfdhmp/errlog"
)

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

	wrapingFunc()

	fmt.Println("End of the program")
}

func wrapingFunc() {
	someBigFunction()
}

func someBigFunction() {
	someSmallFunction()

	if err := someNastyFunction(); errlog.Debug(err) {
		return
	}

	someSmallFunction()
}

func someSmallFunction() {
	fmt.Println("I do things !")
}

func someNastyFunction() error {
	return errors.New("I'm failing for no reason")
}

Output

Console Output examples/basic.go

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

Example

In this example, logrus is used, but any other logger can be used. PrintFunc is of type func (format string, data ...interface{}), so you can easily implement your own logger func. Beware that you should add '\n' at the end of format string when printing.

Now using a custom configuration.

debug := errlog.NewLogger(&errlog.Config{
	PrintFunc:          logrus.Printf,
	LinesBefore:        2,
	LinesAfter:         1,
	PrintError:         true,
	PrintSource:        true,
	PrintStack:         false,
	ExitOnDebugSuccess: true,
})

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

errlog's People

Contributors

chemidy avatar orisano avatar snwfdhmp avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.