Skip to content
/ debugo Public

debugo is a lightweight Go implementation inspired by the popular debug-js library. This package aims to provide simple, human-friendly and foremost fine-graded debugging output for Go developers.

License

Notifications You must be signed in to change notification settings

YoSev/debugo

Repository files navigation

debugo

debugo

debugo is a lightweight Go implementation inspired by the popular debug-js library. This package aims to provide simple, human-friendly and foremost fine-graded debugging output for Go developers.

CI License: MIT

Features

  • Namespace-based (colored) debugging to categorize log output.
  • Toggle debugging on or off via environment variables or programmatically.

Installation

To install debugo, use go get:

go get github.com/yosev/debugo

Usage

Using debugo is straightforward. Here's a basic example:

package main

import (
	"github.com/yosev/debugo"
)

func main() {
	debugo.SetDebug("*") // overwrites DEBUGO env
	debug := debugo.New("my:namespace")

    // debug (using fmt.Sprint)
	debug.Debug("This is a debug message.", 420.69, true, struct {
		Foo string
	}{Foo: "bar"})

    // debug formated (using fmt.Sprintf)
    debug.Debugf("This is a debug message %d %v %v", 420.69, true, struct {
		Foo string
	}{Foo: "bar"})
}

// outputs: my:namespace This is a debug message. 420.69 true {Foo: bar} +0ms

Environment Variables

You can control which debug namespaces are active using the DEBUG environment variable. For example:

export DEBUGO=my:namespace

This will enable debugging for the my:namespace namespace. To enable multiple namespaces, separate them with commas:

export DEBUGO=my:namespace,your:namespace

To enable all namespaces, use:

export DEBUGO=*

Disabling Debugging

To turn off debugging, unset the DEBUGO environment variable:

unset DEBUGO

Configuration

debugo allows you to customize its behavior through the Options struct. These options let you fine-tune how logs are output and processed.

Available Options

type Options struct {
    // Force log output independent of given namespace matching (default: false)
    ForceEnable bool
    // Use background colors over foreground colors (default: false)
    UseBackgroundColors bool
    // Use a static color (github.com/fatih/color) (default: random foreground color)
    Color *color.Color
    // Defines the pipe to output to, eg. stdOut (default: stdErr)
    Output *os.File
    // Write log files in their own go routine (maintains order)
    Threaded bool
    // Enable leading timestamps by adding a time format
    Timestamp *Timestamp
}

Using Options

To create a new logger with specific options, use the NewWithOptions function:

Example

package main

import (
    "github.com/yosev/debugo"
    "github.com/fatih/color"
    "os"
)

func main() {
    options := &debugo.Options{
        ForceEnable:         true,
        UseBackgroundColors: false,
        Color:               color.New(color.FgRed).Add(color.Underline),
        Output:              os.Stdout,
        Threaded:            true,
        Timestamp:           &debugo.Timestamp{Format: time.Kitchen},,
    }

    debug := debugo.NewWithOptions("myapp", options)

    debug.Debug("This is a custom debug message with configured options.")
}

Extend

You can simply extend debugger

debugSign := debugo.New("sign")

// clones into a new debug instance with extended namespace
debugSignIn := debugSign.Extend("in")
debugSignUp := debugSign.Extend("up")
debugSignOut := debugSign.Extend("out")

debugSign.Debug("hello"); // sign hello
debugSignIn.Debug("hello"); // sign:in hello
debugSignUp.Debug("hello"); // sign:up hello
debugSignOut.Debug("hello"); // sign:out hello

Comparison to debug-js

While debugo is inspired by debug-js, it is a simplified version tailored for Go. It does not implement all features of debug-js, focusing on core debugging functionality for Go developers.

Contributing

Contributions are welcome! If you find a bug or have a feature request, feel free to open an issue or submit a pull request.

  1. Fork the repository.
  2. Create a feature branch: git checkout -b my-new-feature.
  3. Commit your changes: git commit -am 'Add some feature'.
  4. Push to the branch: git push origin my-new-feature.
  5. Submit a pull request.

License

debugo is released under the MIT License. See the LICENSE file for details.

Acknowledgments

  • Inspired by debug-js.
  • Thanks to the open-source community for the inspiration and guidance.

About

debugo is a lightweight Go implementation inspired by the popular debug-js library. This package aims to provide simple, human-friendly and foremost fine-graded debugging output for Go developers.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published