diff --git a/log/example_test.go b/log/example_test.go index b07ef8e..809593c 100644 --- a/log/example_test.go +++ b/log/example_test.go @@ -1,7 +1,9 @@ package log_test import ( + "math/rand" "os" + "sync" "time" "github.com/go-kit/kit/log" @@ -96,7 +98,40 @@ 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" +}