log

package
v2.9.1 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package log provides sane default loggers using slog.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	TraceID      = "trace_id"
	SpanID       = "span_id"
	TraceSampled = "trace_sampled"
)

Functions

func JSONHandler

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

JSONHandler is a LogHandlerFn shim for slog.NewJSONHandler.

func NewDefaultEnvLogger

func NewDefaultEnvLogger(opts ...Opt) *slog.Logger

NewDefaultEnvLogger creates new slog.Logger using sane default configuration and sets it as a default logger. Environment variables can be used to configure loggers format and level. Options can be provided to overwrite defaults.

Name: Value: LOG_LEVEL DEBUG|INFO|WARN|ERROR LOG_FORMAT JSON|TEXT

Note: LOG_FORMAT can't be changed at runtime.

Example
package main

import (
	"log/slog"

	"github.com/elisasre/go-common/v2/log"
)

func main() {
	log.NewDefaultEnvLogger()
	slog.Info("Hello world")
	slog.Error("Some error")
}

func ParseLogLevel

func ParseLogLevel(level string) slog.Level

ParseLogLevel turns string into slog.Level using case-insensitive parser. If the input doesn't match to any slog.Level then slog.LevelInfo is used.

func ParseLogLevelFromEnv

func ParseLogLevelFromEnv() slog.Level

ParseLogLevelFromEnv turns LOG_LEVEL env variable into slog.Level using logic from ParseLogLevel.

func ParseSource

func ParseSource(source string) bool

func ParseSourceFromEnv

func ParseSourceFromEnv() bool

func TextHandler

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

TextHandler is a LogHandlerFn shim for slog.NewTextHandler.

Types

type HandlerFn

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

HandlerFn is a shim type for slog's NewHandler functions.

func ParseFormat

func ParseFormat(format string) HandlerFn

ParseFormat parses string into supported log handler function. If the input doesn't match to any supported format then JSON is used.

func ParseFormatFromEnv

func ParseFormatFromEnv() HandlerFn

ParseFormatFromEnv turns LOG_FORMAT env variable into slog.Handler function using ParseFormat.

type Opt

type Opt func(*builder)

func WithGCP added in v2.8.0

func WithGCP(projectID string) Opt

WithGCP configures the logger for Google Cloud Logging. It applies both WithGCPReplacer (with short source paths) and WithGCPTraceContext to produce structured logs compatible with GCP Cloud Logging and Cloud Trace.

The projectID must be the GCP project where Cloud Trace stores spans. In multi-project setups where traces are exported to a central observability project, use that project's ID — not the project where the workload runs.

See https://cloud.google.com/logging/docs/structured-logging#special-payload-fields

func WithGCPReplacer added in v2.1.9

func WithGCPReplacer(short bool) Opt

WithGCPReplacer sets slog.HandlerOptions.ReplaceAttr to GCP structured logging format. It remaps standard slog attribute keys to GCP-specific names:

  • source -> logging.googleapis.com/sourceLocation (with line as string)
  • level -> severity (with GCP severity names)
  • time -> timestamp
  • msg -> message

https://cloud.google.com/logging/docs/structured-logging#special-payload-fields

func WithGCPTraceContext added in v2.8.0

func WithGCPTraceContext(projectID string) Opt

WithGCPTraceContext configures trace context attributes for Google Cloud Logging. It sets the attribute keys to the GCP-specific names and formats the trace ID as "projects/{projectID}/traces/{traceID}" which enables linking between Cloud Logging and Cloud Trace in the GCP console.

The projectID must be the GCP project where Cloud Trace stores spans. In multi-project setups where traces are exported to a central observability project, use that project's ID — not the project where the workload runs.

GCP special fields:

  • logging.googleapis.com/trace
  • logging.googleapis.com/spanId
  • logging.googleapis.com/trace_sampled

See https://cloud.google.com/logging/docs/structured-logging#special-payload-fields

func WithHandlerFn

func WithHandlerFn(h HandlerFn) Opt

WithHandlerFn can be used to provide slog.Handler lazily.

func WithLeveler

func WithLeveler(l slog.Leveler) Opt

WithLeveler sets slog.HandlerOptions.Level.

func WithOutput

func WithOutput(w io.Writer) Opt

WithOutput sets logger's output.

func WithReplacer

func WithReplacer(fn func([]string, slog.Attr) slog.Attr) Opt

WithReplacer sets slog.HandlerOptions.ReplaceAttr. This resets any previously configured replacer functions (from WithShortSource, WithGCPReplacer, etc.) and sets the given function as the sole replacer. Use this as an escape hatch for full control.

func WithShortSource

func WithShortSource(short bool) Opt

WithShortSource sets slog.ReplaceAttr source file as short format.

func WithSource

func WithSource(enabled bool) Opt

WithSource sets slog.HandlerOptions.AddSource.

func WithTraceContext added in v2.8.0

func WithTraceContext(tc TraceContext) Opt

WithTraceContext sets custom trace context configuration for the span context log handler. This allows customizing the attribute keys and value formatters for trace ID, span ID, and trace sampled fields.

type TraceContext added in v2.8.0

type TraceContext struct {
	// TraceIDKey is the attribute key for the trace ID (default: "trace_id").
	TraceIDKey string
	// SpanIDKey is the attribute key for the span ID (default: "span_id").
	SpanIDKey string
	// TraceSampledKey is the attribute key for the trace sampled flag (default: "trace_sampled").
	TraceSampledKey string
	// TraceIDFormatter formats the trace ID string. If nil, the raw hex trace ID is used.
	TraceIDFormatter func(trace.TraceID) string
	// SpanIDFormatter formats the span ID string. If nil, the raw hex span ID is used.
	SpanIDFormatter func(trace.SpanID) string
}

TraceContext configures how OpenTelemetry span context attributes are added to log records.

Jump to

Keyboard shortcuts

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