Skip to content

Commit

Permalink
Merge d522221 into 63d56df
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantv committed Jul 25, 2016
2 parents 63d56df + d522221 commit 10884b4
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package zap_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"

"github.com/uber-go/zap"
Expand Down Expand Up @@ -54,6 +56,36 @@ func Example() {
// {"msg":"Oh no!","level":"error","ts":0,"fields":{"user":"jane@test.com","visits":42}}
}

func Example_fileOutput() {
// Create a temporary file to output logs to.
f, err := ioutil.TempFile("", "log")
if err != nil {
panic("failed to create temporary file")
}
defer os.Remove(f.Name())

logger := zap.NewJSON(
// Write the logging output to the specified file instead of stdout.
// Any type implementing zap.WriteSyncer or zap.WriteFlusher can be used.
zap.Output(f),
)
// Stub the current time in tests.
logger.StubTime()

logger.Info("This is an info log.", zap.Int("foo", 42))

// Sync the file so logs are written to disk, and print the file contents.
f.Sync()
contents, err := ioutil.ReadFile(f.Name())
if err != nil {
panic("failed to read temporary file")
}

fmt.Println(string(contents))
// Output:
// {"msg":"This is an info log.","level":"info","ts":0,"fields":{"foo":42}}
}

func ExampleNest() {
logger := zap.NewJSON()
// Stub the current time in tests.
Expand Down

0 comments on commit 10884b4

Please sign in to comment.