luci: go.chromium.org/luci/common/logging/gkelogger Index | Files

package gkelogger

import "go.chromium.org/luci/common/logging/gkelogger"

Package gkelogger is a logger that outputs all log entries as single-line JSON objects to stdout or stderr.

The JSON format is defined by the Google Cloud Logging plugin for fluentd, which is mainly used by The Google Kubernetes Engine + Stackdriver stack. See https://github.com/GoogleCloudPlatform/fluent-plugin-google-cloud

Index

Package Files

logger.go tracker.go

func Factory Uses

func Factory(w LogEntryWriter, prototype LogEntry) func(context.Context) logging.Logger

Factory returns a factory of logger.Logger instances that log to the given LogEntryWriter (usually &Sink{...}) using the LogEntry (if any) as a prototype for log entries.

For each log message, makes a copy of 'prototype', overwrites its Severity, Message, Time and Fields (keeping other fields as they are), and writes it to LogEntryWriter. This allows to "prepopulate" fields like TraceID or OperationID in all log entries emitted by logging.Logger.

Such factory can be installed in the context via logging.SetFactory.

func FormatTime Uses

func FormatTime(t time.Time) string

FormatTime formats time.Time in a way that is understood by GKE logging.

type LogEntry Uses

type LogEntry struct {
    // Severity is a string denoting the logging level of the entry.
    Severity string `json:"severity"`
    // Message is a single line human readable string of the log message.
    Message string `json:"message,omitempty"`
    // Time is a "sssssssss.nnnnnnnnn" string with the timestamp of the log.
    Time string `json:"time"`
    // TraceID is 32-byte hex string with Stackdriver trace ID.
    TraceID string `json:"logging.googleapis.com/trace,omitempty"`
    // Operation is used to group log lines from a single request together.
    Operation *Operation `json:"logging.googleapis.com/operation,omitempty"`
    // RequestInfo is information about the handled HTTP request.
    RequestInfo *RequestInfo `json:"httpRequest,omitempty"`
    // Fields are extra structured data that may be tagged in the log entry.
    Fields logging.Fields `json:"fields,omitempty"`
}

LogEntry defines a structure of JSON log entries recognized by google-fluentd plugin on GKE.

See https://cloud.google.com/logging/docs/agent/configuration#process-payload for a list of possible magical fields. All other fields are exposed as part of jsonPayload in Stackdriver logs.

type LogEntryWriter Uses

type LogEntryWriter interface {
    // Write appends a log entry to the output.
    Write(*LogEntry)
}

LogEntryWriter knows how to write LogEntries to some output.

type Operation Uses

type Operation struct {
    ID string `json:"id"`
}

Operation is used to group log lines from a single request together.

type RequestInfo Uses

type RequestInfo struct {
    Method       string `json:"requestMethod"` // e.g. "GET"
    URL          string `json:"requestUrl"`    // e.g. "http://host/path"
    Status       int    `json:"status"`        // e.g. 200
    RequestSize  string `json:"requestSize"`   // e.g. "123123"
    ResponseSize string `json:"responseSize"`  // e.g. "2324"
    UserAgent    string `json:"userAgent"`     // e.g. "Mozilla/4.0 ..."
    RemoteIP     string `json:"remoteIp"`      // e.g. "192.168.1.1"
    Latency      string `json:"latency"`       // e.g. "3.5s"
}

RequestInfo contains information about handled HTTP request.

See following definition for all fields we can possibly expose here: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#HttpRequest

type SeverityTracker Uses

type SeverityTracker struct {
    Out LogEntryWriter
    // contains filtered or unexported fields
}

SeverityTracker wraps LogEntryWriter and observes severity of messages there.

func (*SeverityTracker) MaxSeverity Uses

func (s *SeverityTracker) MaxSeverity() string

MaxSeverity returns maximum severity observed thus far or "".

func (*SeverityTracker) Write Uses

func (s *SeverityTracker) Write(l *LogEntry)

Write is part of LogEntryWriter interface.

type Sink Uses

type Sink struct {
    Out io.Writer
    // contains filtered or unexported fields
}

Sink takes care of JSON-serializing log entries and synchronizing writes to an io.Writer (usually stdout or stderr).

Implements LogEntryWriter.

Sink can either be used directly for fine-grain control of what is getting logged, or via a logging.Logger for interoperability with most of LUCI code.

There should be at most one Sink instance assigned to a given io.Writer, shared by all Logger instances writing to it. Violating this requirement may cause malformed log lines.

func (*Sink) Write Uses

func (s *Sink) Write(l *LogEntry)

Write appends a log entry to the output by JSON-serializing and writing it.

Panics if the LogEntry can't be serialized (this should not be possible). Ignores errors from io.Writer.

Package gkelogger imports 9 packages (graph) and is imported by 4 packages. Updated 2019-08-23. Refresh now. Tools for package owners.