只能从Google App Engine的golang应用程序访问请求日志。

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

Can only access request-logs from Google App Engine golang application

问题

我有一个使用GAE的Go语言应用程序,其中的日志语句看起来像这样:

func LogDebugMessage(req *http.Request) {
  context := appengine.NewContext(req)
  c.Debugf("#%d Debugging Here.", 1)
}

我如何访问由c.Debugf调用生成的日志消息?

appcfg.py request_logs和GAE控制台(https://console.developers.google.com/project/.../logs)似乎只有HTTP请求日志。

是否有其他工具可以请求由GAE上下文生成的日志?

英文:

I have a GAE golang application that has logging statements that look something like:

func LogDebugMessage(req *http.Request) {
  context := appengine.NewContext(req)
  c.Debugf("#%d Debugging Here.", 1)
}

How can I access the logging messages generated by the c.Debugf call?

The appcfg.py request_logs and GAE console (https://console.developers.google.com/project/.../logs) both appear to only have HTTP request logs.

Is there another tool to request logs generated by the GAE context?

答案1

得分: 1

有两种类型的日志:请求日志是平台自动生成的,包含请求的详细信息(如时间戳、客户端IP、用户代理、状态码、服务时间、实例ID等),应用日志是通过context.Debugf()context.Warningf()context.Infof()context.Errorf()context.Criticalf()方法调用生成的。

应用日志附加到(或与)请求相关联(请求日志记录),这些请求是在处理这些请求期间调用的。

请求(和应用)日志可以在管理控制台或开发者控制台的日志页面上查看。管理控制台和开发者控制台都显示请求和应用日志记录。请注意,在管理控制台和开发者控制台上,您需要点击日志记录以展开它们。展开请求日志后,将显示从该请求中生成的所有应用日志记录。

如果您想要下载应用日志,可以在appcfg.py工具中指定--include_all参数。

通过编程方式,您可以查询请求日志,请参阅Logs Go API概述了解更多详细信息。这是Log package的参考。

当您查询请求日志时,结果是log.Record值。这是一个包含AppLogs字段的struct,它是log.AppLog的切片。这个AppLog类型(也是一个struct)描述了由context.Debugf()调用等生成的应用日志。

AppLog类型的结构如下:

type AppLog struct {
    Time    time.Time
    Level   int
    Message string
}

Message是格式化日志消息的string结果。

以下是查询包括应用日志的日志记录的示例:

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 {
        c.Infof("Done processing results")
        break
    }
    if err != nil {
        c.Errorf("Failed to retrieve next log: %v", err)
        break
    }
    // 处理记录
    // 处理应用日志:

    for _, ap := range record.AppLogs {
        msg := ap.Message
        // msg是应用日志文本消息
    }
}
英文:

There are 2 kinds of logs: Request logs which are generated automatically by the platform for all requests and contains request details (like timestamp, client ip, user agent, status codes, serving time, instance id etc.) and Application logs which are the result of context.Debugf(), context.Warningf(), context.Infof(), context.Errorf() and context.Criticalf() method calls.

Application logs are attached to (or associated with) requests (request log records), requests from which they were called (during serving those requests).

Request (and application) logs can be viewed on the Logs page of the Admin console or on the Developer console. Both the Admin console and the Developer console display both Request and Application log records. Note that on both the Admin and Developer console you have to click on the log records to expand them. Once you expand a request log, all application log records will be displayed that were done from that request.

If you want to download application logs as well, you can specify the --include_all parameter to the appcfg.py tool.

Programatically you can query the request logs, see Logs Go API Overview for more details. Here is the Log package reference.

When you query request logs, the result are log.Record values. This is a struct which contains an AppLogs field which is a slice of log.AppLog. This AppLog type (it's also a struct) descirbes an application log made by e.g. a context.Debugf() call.

The AppLog type looks like this:

type AppLog struct {
    Time    time.Time
    Level   int
    Message string
}

The Message is the string result of the formatted log message.

Here is an example how to query the log records including Application logs:

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 {
        c.Infof("Done processing results")
        break
    }
    if err != nil {
        c.Errorf("Failed to retrieve next log: %v", err)
        break
    }
    // Do something with record
    // Process Application logs:

    for _, ap := range record.AppLogs {
        msg := ap.Message
        // msg is an application log text message
    }
}

huangapple
  • 本文由 发表于 2015年5月31日 08:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/30552762.html
匿名

发表评论

匿名网友

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

确定