google.golang.org/appengine/log 包 (v1.6.8)

包日志提供了从 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)
}

变量

完成

var Done = errors.New("log: query has no more results")

查询迭代完成时将返回 Done。

函数

func Criticalf

func Criticalf(ctx context.Context, format string, args ...interface{})

Criticalf 类似于 Debugf,但处于严重级别。

func Debugf

func Debugf(ctx context.Context, format string, args ...interface{})

Debugf 根据格式设置其参数的格式,与 fmt.Printf 类似,并在调试级别将文本记录为日志消息。 该消息将与提供的上下文所关联的请求相关联。

func Errorf

func Errorf(ctx context.Context, format string, args ...interface{})

Errorf 类似于 Debugf,但处于错误级别。

func Infof

func Infof(ctx context.Context, format string, args ...interface{})

Infof 类似于 Debugf,但处于信息级别。

func Warningf

func Warningf(ctx context.Context, format string, args ...interface{})

Warningf 类似于 Debugf,但处于警告级别。

AppLog

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

AppLog 表示单个应用级日志。

查询

type Query struct {
	// Start time specifies the earliest log to return (inclusive).
	StartTime time.Time

	// End time specifies the latest log to return (exclusive).
	EndTime time.Time

	// Offset specifies a position within the log stream to resume reading from,
	// and should come from a previously returned Record's field of the same name.
	Offset []byte

	// Incomplete controls whether active (incomplete) requests should be included.
	Incomplete bool

	// AppLogs indicates if application-level logs should be included.
	AppLogs bool

	// ApplyMinLevel indicates if MinLevel should be used to filter results.
	ApplyMinLevel bool

	// If ApplyMinLevel is true, only logs for requests with at least one
	// application log of MinLevel or higher will be returned.
	MinLevel int

	// Versions is the major version IDs whose logs should be retrieved.
	// Logs for specific modules can be retrieved by the specifying versions
	// in the form "module:version"; the default module is used if no module
	// is specified.
	Versions []string

	// A list of requests to search for instead of a time-based scan. Cannot be
	// combined with filtering options such as StartTime, EndTime, Offset,
	// Incomplete, ApplyMinLevel, or Versions.
	RequestIDs []string
}

Query 定义日志查询。

func (*Query) Run

func (params *Query) Run(c context.Context) *Result

Run 为日志记录启动查询,该记录包含请求和应用级日志信息。

Record

type Record struct {
	AppID            string
	ModuleID         string
	VersionID        string
	RequestID        []byte
	IP               string
	Nickname         string
	AppEngineRelease string

	// The time when this request started.
	StartTime time.Time

	// The time when this request finished.
	EndTime time.Time

	// Opaque cursor into the result stream.
	Offset []byte

	// The time required to process the request.
	Latency     time.Duration
	MCycles     int64
	Method      string
	Resource    string
	HTTPVersion string
	Status      int32

	// The size of the request sent back to the client, in bytes.
	ResponseSize int64
	Referrer     string
	UserAgent    string
	URLMapEntry  string
	Combined     string
	Host         string

	// The estimated cost of this request, in dollars.
	Cost              float64
	TaskQueueName     string
	TaskName          string
	WasLoadingRequest bool
	PendingTime       time.Duration
	Finished          bool
	AppLogs           []AppLog

	// Mostly-unique identifier for the instance that handled the request if available.
	InstanceID string
}

Record 包含单个 Web 请求的所有信息。

结果

type Result struct {
	// contains filtered or unexported fields
}

Result 表示查询的结果。

func (*Result) Next

func (qr *Result) Next() (*Record, error)

Next 返回下一个日志记录。