vokerslog

package
v0.0.0-...-717648b Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package vokerslog provides a slog.Handler tuned for AWS Lambda functions.

It auto-configures log format (JSON or text) and level from Lambda's advanced logging environment variables, and enriches every record with Lambda metadata (function name, version, and the request ID from the invocation context). Options override the environment values.

See https://docs.aws.amazon.com/lambda/latest/dg/monitoring-logs.html

Usage:

logger := slog.New(vokerslog.NewHandler(os.Stderr))
slog.SetDefault(logger)
voker.Start(handler, voker.WithLogger(logger))

Index

Constants

View Source
const TypeKey = "type"

TypeKey is the attribute key that overrides a record's "type" field. A top-level (not nested in a group) string attribute with this key sets the type for that record instead of being emitted as an ordinary attribute. It may be set per call or, via WithAttrs, for every record from a logger:

requests := slog.New(handler).With(vokerslog.TypeKey, "app.request")

A per-call value takes precedence over one set via WithAttrs. See WithType.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

Handler implements slog.Handler and writes structured log records to an io.Writer. Use NewHandler to create one.

func NewHandler

func NewHandler(w io.Writer, options ...Option) *Handler

NewHandler returns a Handler that writes to w.

By default it reads AWS_LAMBDA_LOG_LEVEL (TRACE, DEBUG, INFO, WARN, ERROR, FATAL) and AWS_LAMBDA_LOG_FORMAT (json, text) from the environment. Any provided options take precedence over environment values.

func (*Handler) Enabled

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

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, record 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

type Option

type Option func(*Handler)

Option configures a Handler. Pass options to NewHandler.

func WithJSON

func WithJSON() Option

WithJSON sets the output format to JSON.

func WithLevel

func WithLevel(level slog.Leveler) Option

WithLevel sets the minimum log level. Messages below this level are dropped. Defaults to the value of AWS_LAMBDA_LOG_LEVEL, or INFO if unset.

func WithSource

func WithSource() Option

WithSource adds caller information (function, file, line) to each log record.

func WithText

func WithText() Option

WithText sets the output format to key=value text.

func WithType

func WithType(logType string) Option

WithType sets the "type" field included in every log record. Defaults to "app.log".

The "type" + "record" envelope mirrors the shape of AWS Lambda Telemetry API events, so "type" is best used as a low-cardinality category for filtering and routing logs (for example in CloudWatch Logs Insights), not for per-request data. AWS reserves the values "function", "extension", and "platform.*"; a dotted "app.<category>" namespace (such as "app.log", "app.request", or "app.audit") avoids collisions and matches AWS's style.

An individual record can override this default with a top-level string attribute named TypeKey; setting it to "" omits the field for that record.

func WithoutTime

func WithoutTime() Option

WithoutTime omits the timestamp from log output. Useful for testing or when CloudWatch already provides timestamps.

Jump to

Keyboard shortcuts

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