Package documentation: https://godoc.org/github.com/JonahBraun/dog
Import this library and create the log object, configured with the minimum log level to display:
import "github.com/JonahBraun/dog"
var log = dog.NewDog(dog.DEBUG)
Then somewhere in a function:
log.Warn("some warning", ...optionalVarsToDisplay)
package main
import "github.com/JonahBraun/dog"
var log = dog.NewDog(dog.DEBUG)
func main() {
number := 8
msg := "error message from some library"
log.Debug("needed for advanced debugging")
// variable(s) can be appended to any of the log calls
log.Info("Your number is", number)
log.Warn("Possibly an issue")
log.Err("This should't be happening…", msg)
// Fatal returns os.Exit(int) so that you can easily exit
log.Fatal("Fatal, must exit")(1)
}
The above code will show:
Note the final line in the screenshot is go run
printing a non 0 exit status and not something Dog is printing.
Five logging levels are defined: debug, info, warn, err, fatal. A log level is only displayed if it is at or above the level given when creating the dog object. For example, in response to a quiet flag you could only print err and fatal messages:
dog.NewDog(dog.ERR)
You can customize the appearance of a log level by assigning a new log function with CreateLog(color string, prefix string):
log.Warn = dog.CreateLog(dog.FgRed, "> ")
log.Warn("Typical customization")
Color ANSI codes are defined to make this convenient. Because CreateLog just concatenates the color and prefix strings, you can do anything you want:
log.Warn = dog.CreateLog("", dog.FgRed+">"+dog.TR+" ")
log.Warn("Even more minimal")
log.Warn = dog.CreateLog(dog.FgYellow+dog.Reverse, "🐺 ")
log.Warn("Dawg, this custom fatal log format is rockin the CLI!!1")
This library was originally written for Wago, a file watcher / build tool, which requires log messages to be easily distinguishable from the application output being monitored.