Categorygithub.com/ccremer/plogr
repositorypackage
0.7.0
Repository: https://github.com/ccremer/plogr.git
Documentation: pkg.go.dev

# README

plogr

Go version Version Go Report Card Codecov

go-logr implementation with pterm

Usage

See examples

Add more colors and levels

By default, only level 0 (info) and level 1 (debug) are configured.

While go-logr doesn't have names for logging levels ("info", "warning", etc), pterm does. Both libraries agree that verbosity is increased with higher numeric logging level. For pterm however, one could argue that their "Warning" and "Success" levels have lower verbosity than "Info". If you like to customize the levels or styling, you could do something like this:

func main() {
	sink := plogr.NewPtermSink().
	    SetLevelPrinter(3, pterm.Debug).
        SetLevelPrinter(2, plogr.DefaultLevelPrinters[0]).
        SetLevelPrinter(1, pterm.Success).
        SetLevelPrinter(0, pterm.Warning)

	logger := logr.New(sink)
	logger.V(0).WithName("main").Info("Warning message")
	logger.V(1).WithName("app").Info("Success message")
	logger.V(2).WithName("database").Info("Info message", "key", "value")
	logger.V(3).WithName("controller").Info("Debug message")
}

example output

That means whenever you want to show informational messages, you'd have to set the logging level to 2 in your code base. Alternatively, without breaking the code base, append levels like success to the LevelPrinters to your desired level, disregarding the verbosity increase though. For a logging library, it remains questionable though if you'd like to maintain warnings and success levels via plogr or whether they're better explicitly invoked in pterm.

The error printer can be customized as well, but it has its own field.