gcp

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2025 License: Apache-2.0 Imports: 11 Imported by: 9

README

clog/gcp: structured logging for Google Cloud using slog

Contrary to the documented "standard" approach for logging, this doesn't use any third-party logging package for logging.

Instead, it relies on Google Cloud's support for ingesting structured logs by simply printing JSON to stderr.

This method of emitting structured logs is supported by:

Basic Usage

To use this, underscore-import gcp/init, which will configure slog to use the GCP-optimized JSON handler for all log messages:

Then when you use slog, all log messages will be output in JSON format to standard error, which is automatically ingested by Cloud Logging.

import _ "github.com/chainguard-dev/clog/gcp/init"

...

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  slog.InfoContext(r.Context(), "my message",
    "mycount", 42,
    "mystring", "myvalue",
  )
})

This logs the message, with the additional structured logging fields in Cloud Logging:

Correlating Logs with Requests

You can also use this to correlate log lines with the request that generated them, by associating the log message with the request's trace context header.

import "github.com/chainguard-dev/clog/gcp"

...

http.Handle("/", gcp.WithCloudTraceContext(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  slog.InfoContext(r.Context(), "my message",
    "mycount", 42,
    "mystring", "myvalue",
  )
})))

This logs the message, associated with the request's trace, in Cloud Logging:

Other logs with the same trace attribute are generated by the same incoming request.

See https://cloud.google.com/trace/docs/trace-log-integration for more information.

Critical Logging

Cloud Logging supports a CRITICAL logging level, which doesn't map cleanly to slog's built-in levels.

To log at this level:

slog.Log(ctx, gcp.LevelCritical, "I have a bad feeling about this...")

See ./cmd/example for a deployable example.


This repo is forked from https://github.com/remko/cloudrun-slog, which originated this idea and implementation.

Documentation

Index

Constants

View Source
const LevelCritical = slog.Level(12)

LevelCritical is an extra log level supported by Cloud Logging.

Variables

This section is empty.

Functions

func WithCloudTraceContext

func WithCloudTraceContext(h http.Handler) http.Handler

WithCloudTraceContext returns an http.handler that adds the GCP Cloud Trace ID to the context. This is used to correlate the structured logs with the request log.

Types

type Handler

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

Handler that outputs JSON understood by the structured log agent. See https://cloud.google.com/logging/docs/agent/logging/configuration#special-fields

func NewHandler

func NewHandler(level slog.Level) *Handler

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, rec slog.Record) error

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL