• Stars
    star
    450
  • Rank 97,143 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Reduce debugging time while programming Go. Use static and stack-trace analysis to determine which func call causes the error.

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

Example

Buy Me A Coffee

Introduction

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

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ย 
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.

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

More Repositories

1

awesome-gpt-prompt-engineering

A curated list of awesome resources, tools, and other shiny things for GPT prompt engineering.
Python
584
star
2

simplehttp

Simple and lightweight http server for local files
Go
54
star
3

duck

Command snippet framework w/ repository (cloud & self hosted)
Go
40
star
4

octopus

Decentralized network mesh toolkit โœฝ
Go
21
star
5

llm

Use any LLM from the command line.
JavaScript
21
star
6

taskr

Automated testing: No-regression implementation using git hooks
Go
17
star
7

create-react-chrome-extension

Chrome / Firefox extension template using React and state-of-the-art settings.
JavaScript
16
star
8

prose

Chatbot framework powered by regular expressions
Go
10
star
9

efs

Encrypted File System over HTTP (using Ed25519 HMAC auth and AES-256 encryption).
Go
7
star
10

decode

Helpers to parse JSON/YAML files into Golang structs
Go
5
star
11

dnsrecon

Easy to use DNS recon tool that uses prefix dictionary.
Shell
4
star
12

population-genome-evolution

Library for simulating the evolution of a population'sย genome through reproductions, by applying genes mutations and cross-overs.
Shell
3
star
13

learnhub

Web application for sharing documents inside my engineering school (Centrale Lille).
PHP
3
star
14

blindtest

Blindtest platform synced with Spotify.
JavaScript
2
star
15

opendev

Previous implementation of taskr' testing and anti-regression principles
Go
2
star
16

goshell

BSD Shell emulator written in Golang. First project using Golang, practice.
Go
2
star
17

Textual-RPG

A texual RPG written in C++
C++
2
star
18

dockergate

Lightweight cloud server utility to run docker container and forward ports to them.
Go
2
star
19

avalam-ai-game

Board game enhanced with AI strategy
C++
2
star
20

tv-search

API to search for torrents for any given TV show. Easy deploy to docker.
JavaScript
2
star
21

duck-core

Duck's core remote repository. Hosts most standard commands you like to quickstart with.
2
star
22

hashcode2019

My Solution for Google's Hashcode 2019 (Algorithmic Contest) (highly-efficient multi-thread randomiser and superviser)
Go
2
star
23

hashcode-2020

Google's Hashcode 2020 Ordoclic Team Solution (Algorithmic Contest)
Go
2
star
24

Moulinette

Simple algorithm for code quality notation
C++
1
star
25

unipdf-old

Fixes unidoc/unipdf issue with glide. I do not own the code. See original repo.
Go
1
star
26

yt

Simple shell func for quick YouTube searching (CLI)
Shell
1
star
27

httpmirror

Http utility server that instantly writes received request to stdout.
Go
1
star
28

dsnet

first implementation of DSNet protocol
Go
1
star
29

gpt-prompts

We need a central repository to store them all and allow multiple generation version upgrades
1
star
30

cloudbase

Plug-and-play data storage able to perform any basic CRUD operations on any dynamically given type of data.
Go
1
star
31

compression

Software Engineer studies: Implement Huffman compression algorithm in C
C
1
star
32

nuts

Nuts' Dockerfile was broken so I fixed it and uploaded here
JavaScript
1
star
33

findpath

Labyrinth challenger training toolkit library
Go
1
star
34

test-taskr-back

Tests for taskr SaaS back-end link to GitHub
Go
1
star
35

dog

Easily create and use your own boilerplates with Go templating syntax #todo-readme
Go
1
star
36

tmpl

Lightweight package for using Go's templating system outside Go (eg: in a shell).
Go
1
star
37

trash

Practice project for web techs implementing social network features
1
star
38

hashcode2021

JavaScript
1
star
39

blockchain

My own implementation of a blockchain, in Go.
Go
1
star
40

server-install

Shell
1
star
41

nas-ingress

1
star
42

snwfdhmp

1
star
43

gpt-chrome-extension

Chat with GPT about current webpage content. (Summarize & Ask)
JavaScript
1
star
44

pizza

Trying to re-write pizzaman API using Buffalo framework
Go
1
star
45

neural-networks

Artificial neural networks that performs number recognition functions using FANN library
C++
1
star