google-app-engine - c.Infof undefined (type context.Context has no field or method Infof) google.golang.org/appengine/log 错误

标签 google-app-engine go

在 Go Runtime 中,我使用方法 c.Infof 来记录消息,但编译失败并出现以下错误 c.Infof 未定义(类型 context.Context 没有字段或方法 Infof)。 错误清楚地表明从 c := appengine.NewContext(r) 返回的应用引擎上下文是 context.Context 类型 并且它上面没有方法 c.Infof。但与此相反的是 https://godoc.org/google.golang.org/appengine/log 中的文档表明存在这种方法。还有一点需要注意,该方法存在于“appengine”(导入“appengine”)包返回的上下文中,而这似乎不存在于新包 google.golang.org/返回的上下文中appengine ,什么是 c.Infof 等同于类型为 context.Context 的新上下文,由 package "google 返回.golang.org/appengine” ?

最佳答案

包文档中的示例不正确。

使用 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 团队。

关于google-app-engine - c.Infof undefined (type context.Context has no field or method Infof) google.golang.org/appengine/log 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29168848/

相关文章:

go - 如何防止 http 客户端进行 TLS 重新协商?

google-app-engine - GAE 内存缓存生命周期非常短

google-app-engine - 我将如何在 Go 中的 App Engine 上实现一对多?

golang linter 总是提示

go - 在 Golang 中读取 Zlib 压缩文件的最有效方法?

Golang 在包之间共享配置

使用指针引用去测试函数

python - Google App Engine (Python) 中的 AES 加密和 iOS (Objective-C) 中的解密

java - 分片还是不分片? GAE/java/jdo

javascript - 使用长服务器端脚本加载网页的最佳实践