Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions log/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,27 @@
// handled atomically within the wrapped logger, but it typically serializes
// both the formatting and output logic. Use a SyncLogger if the formatting
// logger may perform multiple writes per log event.
//
// Error Handling
//
// This package relies on the practice of wrapping or decorating loggers with
// other loggers to provide composable pieces of functionality. It also means
// that Logger.Log must return an error because some
// implementations—especially those that output log data to an io.Writer—may
// encounter errors that cannot be handled locally. This in turn means that
// Loggers that wrap other loggers should return errors from the wrapped
// logger up the stack.
//
// Fortunately, the decorator pattern also provides a way to avoid the
// necessity to check for errors every time an application calls Logger.Log.
// An application required to panic whenever its Logger encounters
// an error could initialize its logger as follows.
//
// fmtlogger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
// logger := log.LoggerFunc(func(keyvals ...interface{}) error {
// if err := fmtlogger.Log(keyvals...); err != nil {
// panic(err)
// }
// return nil
// })
package log
41 changes: 38 additions & 3 deletions log/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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoa, didn't know that was a thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peterbourgon It was added in Go 1.7 but I was surprised to discover it missing from the docs at golang.org/pkg/testing. There is an oblique reference in the release notes, but the best place to find it is by running go help testfunc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad I could help.. I had basically the exact same issue with a library I wrote a while back so I figured it would be useful.. the deploy became complicated because godoc has to be backwards compatible so that kind of became a crap show for deployments. =(

// taskID=1 event="starting task"
// taskID=2 event="starting task"
// taskID=1 event="task complete"
// taskID=2 event="task complete"
}