Logger.SetPrefix()是否在上下文之间停留在通道/线程之间?

When I've used log packages for other languages, I always enforced some type of contextual Guid (UUID) that gets logged with each logger call. Specifically, this really helps track down which group of logs belong to what web request, or individual thread, when logging 1000s of requests.

I am attempting to do this with the std logger that comes with Go.

type Context struct {
  Log *log.Logger
}

// NewContext constructs a new context.
func NewContext(r *http.Request) (*Context, error) {

    id, err := newUUID()
    if err != nil {
        log.Printf("ERROR in newUUID() : %s", err)
    }

    c := &Context{
        Log: log.New(os.Stderr, id+" ", log.LstdFlags)
    }

    return c, nil
}

func newUUID() (string, error) {
    uuid := make([]byte, 16)
    n, err := io.ReadFull(rand.Reader, uuid)
    if n != len(uuid) || err != nil {
        return "", err
    }
    // variant bits; see section 4.1.1
    uuid[8] = uuid[8]&^0xc0 | 0x80
    // version 4 (pseudo-random); see section 4.1.3
    uuid[6] = uuid[6]&^0xf0 | 0x40
    return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}

As you can see, I assign a log.Logger to a value on the struct.

It is used through my defaultHandler(), as well as within other handlers, like this example:

func defaultHandler(fn func(http.ResponseWriter, *http.Request, *Context) error) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {

        // create the context
        c, err := NewContext(r)
        if err != nil {
            log.Printf("ERROR in creating context w/NewContext(): %s", http.StatusInternalServerError, err.Error())
        }
        defer c.Unload()

        c.Log.Printf("METRIC, START URL: %s", r.URL.Path)
    }
}

Note the c.Log.Printf() call for the logger.

Which outputs something like this:

8c93fa699f5a46c1a986076b952f5c2c 2014/07/13 22:45:21 METRIC, START URL: /

I did this because I was not sure how the following works in the context of channels and sync:

log.SetPrefix("...")

Could someone with more experience with log.SetPrefix() explain how it works in regards to channels and threads, specifically in an http request?

Trying to avoid creating a new logger on each request. Would prefer to use the standard global log Logger from the "log" package, with the .SetPrefix("...").

Or, perhaps outline another solution?

You can't use the SetPrefix method. That will set the .prefix attribute in the global logger. All the handlers share the same logger.

The way you are doing it right now is one way to do it. Another way is to add a log Method to your context:

type Context struct {
    UUID string
}

func (c *Context) Info(fmt string, args ...interface{}) {
    log.Printf(c.UUID+" "+fmt, args...)
}

Full working example

package main

import (
    "crypto/rand"
    "fmt"
    "io"
    "log"
)

func newUUID() (string, error) {
    uuid := make([]byte, 16)
    n, err := io.ReadFull(rand.Reader, uuid)
    if n != len(uuid) || err != nil {
        return "", err
    }
    // variant bits; see section 4.1.1
    uuid[8] = uuid[8]&^0xc0 | 0x80
    // version 4 (pseudo-random); see section 4.1.3
    uuid[6] = uuid[6]&^0xf0 | 0x40
    return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}

type Context struct {
    UUID string
}

func (c *Context) Info(fmt string, args ...interface{}) {
    log.Printf(c.UUID+" "+fmt, args...)
}

func main() {
    uuid, err := newUUID()
    if err != nil {
        log.Fatal(err)
    }
    c := &Context{UUID: uuid}
    c.Info("Hello ")
}

http://play.golang.org/p/uEIKweC-kp