Add Print and Printf top level methods

This commit is contained in:
Olivier Poitrey 2017-09-01 19:56:35 -07:00
parent 96c2125038
commit 8c682b3b12
4 changed files with 53 additions and 1 deletions

View File

@ -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")

17
log.go
View File

@ -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) {

View File

@ -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 {

View File

@ -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)