sheldonhull / plogr

go-logr implementation with pterm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

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()
	sink.LevelPrinters[3] = pterm.Debug
	sink.LevelPrinters[2] = plogr.DefaultLevelPrinters[0]
	sink.LevelPrinters[1] = pterm.Success
	sink.LevelPrinters[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.

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

About

go-logr implementation with pterm

License:Apache License 2.0


Languages

Language:Go 93.9%Language:Makefile 6.1%