dsapkqaduj6718493 2014-07-14 02:50 采纳率: 100%
浏览 68
已采纳

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?

  • 写回答

1条回答 默认 最新

  • douzi1117 2014-07-14 03:09
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源