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.
- Namespace-based (colored) debugging to categorize log output.
- Toggle debugging on or off via environment variables or programmatically.
To install debugo
, use go get
:
go get github.com/yosev/debugo
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
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=*
To turn off debugging, unset the DEBUGO
environment variable:
unset DEBUGO
debugo
allows you to customize its behavior through the Options
struct. These options let you fine-tune how logs are output and processed.
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
}
To create a new logger with specific options, use the NewWithOptions
function:
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.")
}
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
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.
Contributions are welcome! If you find a bug or have a feature request, feel free to open an issue or submit a pull request.
- Fork the repository.
- Create a feature branch:
git checkout -b my-new-feature
. - Commit your changes:
git commit -am 'Add some feature'
. - Push to the branch:
git push origin my-new-feature
. - Submit a pull request.
debugo
is released under the MIT License. See the LICENSE file for details.
- Inspired by debug-js.
- Thanks to the open-source community for the inspiration and guidance.