diff --git a/README.md b/README.md index c54ca01..f0872b4 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Zerolog's API is designed to provide both a great developer experience and stunn The uber's [zap](https://godoc.org/go.uber.org/zap) library pioneered this approach. Zerolog is taking this concept to the next level with simpler to use API and even better performance. -To keep the code base and the API simple, zerolog focuses on JSON logging only. Pretty logging on the console is made possible using the provided (but slower) `zerolog.ConsoleWriter`. +To keep the code base and the API simple, zerolog focuses on JSON logging only. Pretty logging on the console is made possible using the provided (but inefficient) `zerolog.ConsoleWriter`. ![](pretty.png) @@ -31,6 +31,13 @@ import "github.com/rs/zerolog/log" ### A global logger can be use for simple logging +```go +log.Print("hello world") + +// Output: {"level":"debug","time":1494567715,"message":"hello world"} +``` + + ```go log.Info().Msg("hello world") diff --git a/log.go b/log.go index 426882b..de82089 100644 --- a/log.go +++ b/log.go @@ -68,6 +68,7 @@ package zerolog import ( + "fmt" "io" "io/ioutil" "os" @@ -289,6 +290,22 @@ func (l Logger) Log() *Event { return l.newEvent(PanicLevel, false, nil) } +// Print sends a log event using debug level and no extra field. +// Arguments are handled in the manner of fmt.Print. +func (l Logger) Print(v ...interface{}) { + if e := l.Debug(); e.Enabled() { + e.Msg(fmt.Sprint(v...)) + } +} + +// Printf sends a log event using debug level and no extra field. +// Arguments are handled in the manner of fmt.Printf. +func (l Logger) Printf(format string, v ...interface{}) { + if e := l.Debug(); e.Enabled() { + e.Msg(fmt.Sprintf(format, v...)) + } +} + // Write implements the io.Writer interface. This is useful to set as a writer // for the standard library log. func (l Logger) Write(p []byte) (n int, err error) { diff --git a/log/log.go b/log/log.go index 1036d85..2b60244 100644 --- a/log/log.go +++ b/log/log.go @@ -84,6 +84,18 @@ func Log() *zerolog.Event { return Logger.Log() } +// Print sends a log event using debug level and no extra field. +// Arguments are handled in the manner of fmt.Print. +func Print(v ...interface{}) { + Logger.Print(v...) +} + +// Printf sends a log event using debug level and no extra field. +// Arguments are handled in the manner of fmt.Printf. +func Printf(format string, v ...interface{}) { + Logger.Printf(format, v...) +} + // Ctx returns the Logger associated with the ctx. If no logger // is associated, a disabled logger is returned. func Ctx(ctx context.Context) *zerolog.Logger { diff --git a/log_example_test.go b/log_example_test.go index a9a3f14..4991375 100644 --- a/log_example_test.go +++ b/log_example_test.go @@ -49,6 +49,22 @@ func ExampleLogger_Sample() { // {"level":"info","message":"message 4"} } +func ExampleLogger_Print() { + log := zerolog.New(os.Stdout) + + log.Print("hello world") + + // Output: {"level":"debug","message":"hello world"} +} + +func ExampleLogger_Printf() { + log := zerolog.New(os.Stdout) + + log.Printf("hello %s", "world") + + // Output: {"level":"debug","message":"hello world"} +} + func ExampleLogger_Debug() { log := zerolog.New(os.Stdout)