Skip to content

Commit

Permalink
Add Print and Printf top level methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Sep 2, 2017
1 parent 96c2125 commit 8c682b3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

Expand Down
17 changes: 17 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
package zerolog

import (
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions log_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 8c682b3

Please sign in to comment.