c.Infof undefined (type context.Context has no field or method Infof) google.golang.org/appengine/log error

huangapple go评论69阅读模式
英文:

c.Infof undefined (type context.Context has no field or method Infof) google.golang.org/appengine/log error

问题

在Go Runtime中,我使用了方法c.Infof来记录消息,但它在编译时失败,并显示以下错误:
c.Infof未定义(类型context.Context没有字段或方法Infof)
错误明确告诉我们,从c := appengine.NewContext(r)返回的app engine上下文是类型为context.Context的,并且它没有一个名为c.Infof的方法。但与此相反,https://godoc.org/google.golang.org/appengine/log 中的文档表明该方法是存在的。还要注意的一点是,该方法存在于由"appengine"(import "appengine")包返回的上下文中,而在由新包google.golang.org/appengine返回的上下文中似乎不存在,那么在由***"google.golang.org/appengine"***包返回的类型为context.Context的新上下文中,c.Infof等效于什么?

英文:

In the Go Runtime i used the method c.Infof to log messages , but it fails to compile with the following error
c.Infof undefined (type context.Context has no field or method Infof) .
The Error clearly tells that the app engine context returned from c := appengine.NewContext(r) is of type context.Context and it doesnt have a method c.Infof on it. But contrary to this the documentation in https://godoc.org/google.golang.org/appengine/log suggests that this method exists . Another point to note , The method existed on the context returned by "appengine" (import "appengine" ) package , and this doesnt seem to exist on the context returned by the new package google.golang.org/appengine , what is c.Infof equivalent on the new Context of type context.Context returned by package "google.golang.org/appengine" ?

答案1

得分: 3

包文档中的示例是不正确的。

请使用log包的函数来写入App Engine日志。以下是修正后的示例:

c := appengine.NewContext(r)
query := &log.Query{
    AppLogs:  true,
    Versions: []string{"1"},
}

for results := query.Run(c); ; {
    record, err := results.Next()
    if err == log.Done {
        log.Infof(c, "Done processing results")
        break
    }
    if err != nil {
        log.Errorf(c, "Failed to retrieve next log: %v", err)
        break
    }
    log.Infof(c, "Saw record %v", record)
}

包文档中的示例是从App Engine Classic包中复制过来的,但没有更新为使用新的函数。我建议将此问题报告给App Engine团队。

英文:

The example in the package documentation is not correct.

Use the log package functions to write to the App Engine log. Here's the corrected example:

c := appengine.NewContext(r)
query := &log.Query{
    AppLogs:  true,
    Versions: []string{"1"},
}

for results := query.Run(c); ; {
    record, err := results.Next()
    if err == log.Done {
	   log.Infof(c, "Done processing results")
	   break
    }
    if err != nil {
	    log.Errorf(c, "Failed to retrieve next log: %v", err)
	    break
    }
    log.Infof(c, "Saw record %v", record)
}

The example in the package documentation was copied from the App Engine Classic package, but not updated to use the new functions. I suggest reporting this to the App Engine Team.

huangapple
  • 本文由 发表于 2015年3月20日 22:25:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/29168848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定