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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度