GithubHelp home page GithubHelp logo

go-debug's Introduction

go-debug

Conditional debug logging for Go libraries.

View the docs.

Installation

$ go get github.com/tj/go-debug

Example

package main

import . "github.com/tj/go-debug"
import "time"

var debug = Debug("single")

func main() {
  for {
    debug("sending mail")
    debug("send email to %s", "[email protected]")
    debug("send email to %s", "[email protected]")
    debug("send email to %s", "[email protected]")
    time.Sleep(500 * time.Millisecond)
  }
}

If you run the program with the DEBUG=* environment variable you will see:

15:58:15.115 34us   33us   single - sending mail
15:58:15.116 3us    3us    single - send email to [email protected]
15:58:15.116 1us    1us    single - send email to [email protected]
15:58:15.116 1us    1us    single - send email to [email protected]
15:58:15.620 504ms  504ms  single - sending mail
15:58:15.620 6us    6us    single - send email to [email protected]
15:58:15.620 4us    4us    single - send email to [email protected]
15:58:15.620 4us    4us    single - send email to [email protected]
15:58:16.123 503ms  503ms  single - sending mail
15:58:16.123 7us    7us    single - send email to [email protected]
15:58:16.123 4us    4us    single - send email to [email protected]
15:58:16.123 4us    4us    single - send email to [email protected]
15:58:16.625 501ms  501ms  single - sending mail
15:58:16.625 4us    4us    single - send email to [email protected]
15:58:16.625 4us    4us    single - send email to [email protected]
15:58:16.625 5us    5us    single - send email to [email protected]

A timestamp and two deltas are displayed. The timestamp consists of hour, minute, second and microseconds. The left-most delta is relative to the previous debug call of any name, followed by a delta specific to that debug function. These may be useful to identify timing issues and potential bottlenecks.

The DEBUG environment variable

Executables often support --verbose flags for conditional logging, however libraries typically either require altering your code to enable logging, or simply omit logging all together. go-debug allows conditional logging to be enabled via the DEBUG environment variable, where one or more patterns may be specified.

For example suppose your application has several models and you want to output logs for users only, you might use DEBUG=models:user. In contrast if you wanted to see what all database activity was you might use DEBUG=models:*, or if you're love being swamped with logs: DEBUG=*. You may also specify a list of names delimited by a comma, for example DEBUG=mongo,redis:*.

The name given should be the package name, however you can use whatever you like.

License

MIT

go-debug's People

Contributors

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