Skip to content

Commit

Permalink
log: Add NewSyncWriter example.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHines committed Jan 11, 2017
1 parent 108e569 commit 5cf3a36
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package log_test

import (
"math/rand"
"os"
"sync"
"time"

"github.com/go-kit/kit/log"
Expand Down Expand Up @@ -96,7 +98,40 @@ func Example_debugInfo() {
logger.Log("call", "third")

// Output:
// time=2015-02-03T10:00:01Z caller=example_test.go:91 call=first
// time=2015-02-03T10:00:02Z caller=example_test.go:92 call=second
// time=2015-02-03T10:00:03Z caller=example_test.go:96 call=third
// time=2015-02-03T10:00:01Z caller=example_test.go:93 call=first
// time=2015-02-03T10:00:02Z caller=example_test.go:94 call=second
// time=2015-02-03T10:00:03Z caller=example_test.go:98 call=third
}

func Example_syncWriter() {
w := log.NewSyncWriter(os.Stdout)
logger := log.NewLogfmtLogger(w)

type Task struct {
ID int
}

var wg sync.WaitGroup

RunTask := func(task Task, logger log.Logger) {
logger.Log("taskID", task.ID, "event", "starting task")

time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond)

logger.Log("taskID", task.ID, "event", "task complete")
wg.Done()
}

wg.Add(2)

go RunTask(Task{ID: 1}, logger)
go RunTask(Task{ID: 2}, logger)

wg.Wait()

// Unordered output:
// taskID=1 event="starting task"
// taskID=2 event="starting task"
// taskID=1 event="task complete"
// taskID=2 event="task complete"
}

0 comments on commit 5cf3a36

Please sign in to comment.