gcplog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 13 Imported by: 0

README

gcplog

GoDoc

This package contains slog handler that can be used to log to stdout in a format that is compatible with Google Cloud Logging.

NewAutoHandler also falls back to console logging if the environment is not GCP.

Example

package main

import (
	"fmt"
	"log/slog"
	"os"

	"github.com/mycujoo/go-stdlib/pkg/gcplog"
)

func main() {
    h := gcplog.NewAutoHandler(os.Stderr, &gcplog.HandlerOptions{
        AddSource:      true,
        ServiceName:    "some-service",
        ReportErrors:   true,
    })
    
    err := fmt.Errorf("storage.Get: %w", os.ErrNotExist)
    
    logger := slog.New(h)
    logger.Error("operation failed", gcplog.Error(err))
}

Service name can be set through options or from environment variable GCPLOG_SERVICE_NAME. Fallback to OTEL_SERVICE_NAME is supported.

To automatically set your service version during build you can use following command:

go build -ldflags "-X github.com/mycujoo/go-stdlib/pkg/gcplog.serviceVersion=$(git rev-parse HEAD)" -o ./bin/server ./cmd/server

Alternatively you can set GCPLOG_SERVICE_VERSION environment variable.

It is based on slogdriver package, but has some changes:

  1. Integrated with open telemetry directly.
  2. Trace context is optional.
  3. Labels removed.
  4. Added service context.
  5. Support for cloud error reporting.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(err error) slog.Attr

Error wraps an error in a slog.Attr with a standard key.

func NewAutoHandler

func NewAutoHandler(w io.Writer, opts *HandlerOptions) slog.Handler

NewAutoHandler returns slog.Handler that writes to w using GCP structured logging format. It automatically detects GCP project ID. If the program is not running on GCE, it returns console handler.

Example
package main

import (
	"fmt"
	"log/slog"
	"os"

	"github.com/mycujoo/go-stdlib/pkg/gcplog"
)

func main() {
	h := gcplog.NewAutoHandler(os.Stderr, &gcplog.HandlerOptions{
		AddSource:    true,
		ServiceName:  "some-service",
		ReportErrors: true,
	})

	err := fmt.Errorf("storage.Get: %w", os.ErrNotExist)

	logger := slog.New(h)
	logger.Error("operation failed", gcplog.Error(err))
}
Output:

func NewReportContext

func NewReportContext(pc uintptr) slog.Attr

NewReportContext creates a new report context. see: https://cloud.google.com/error-reporting/docs/formatting-error-messages

Types

type Handler

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

func NewHandler

func NewHandler(w io.Writer, opts *HandlerOptions) *Handler

NewHandler returns slog.Handler that writes to w using GCP structured logging format.

func (*Handler) Enabled

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

func (*Handler) Handle

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

func (*Handler) WithAttrs

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

func (*Handler) WithGroup

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

type HandlerOptions

type HandlerOptions struct {
	// AddSource causes the handler to compute the source code position
	// of the log statement and add a SourceKey attribute to the output.
	AddSource bool

	// Minimal log level to log, defaults to slog.LevelInfo
	Level slog.Leveler

	// Service name and version to add to the log
	ServiceName    string
	ServiceVersion string

	// If this is set to true, errors will be reported to GCP error reporting.
	ReportErrors bool

	// GCP project ID to use for trace context
	GCPProjectID string
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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