sloglambda

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: Unlicense Imports: 14 Imported by: 1

README

slog-lambda

current codecov Doc License

AWS Lambda slog.Handler

A log/slog handler for AWS Lambda advanced logging controls.

Features
  • Auto-configures log format and level from Lambda environment variables (AWS_LAMBDA_LOG_FORMAT, AWS_LAMBDA_LOG_LEVEL)
  • JSON and text output formats
  • Includes Lambda metadata (function name, version, request ID) in log output
  • Supports custom log levels: TRACE and FATAL
  • Source code location in log records
Install
go get github.com/maddiesch/slog-lambda
Usage
package main

import (
	"context"
	"log/slog"
	"os"

	"github.com/aws/aws-lambda-go/lambda"
	sloglambda "github.com/maddiesch/slog-lambda"
)

func init() {
	logger := slog.New(sloglambda.NewHandler(os.Stdout))
	slog.SetDefault(logger)
}

func main() {
	lambda.Start(func(ctx context.Context, event any) error {
		slog.InfoContext(ctx, "Lambda Invoked", slog.Any("event", event))

		return nil
	})
}

By default, NewHandler reads AWS_LAMBDA_LOG_FORMAT and AWS_LAMBDA_LOG_LEVEL to configure itself. Use options to override:

handler := sloglambda.NewHandler(os.Stdout,
	sloglambda.WithJSON(),
	sloglambda.WithLevel(slog.LevelDebug),
	sloglambda.WithSource(),
	sloglambda.WithType("api.request"),
)
Options
Option Description
WithJSON() Output in JSON format
WithText() Output in text format
WithLevel(slog.Leveler) Set the minimum log level
WithSource() Include source file, function, and line number
WithType(string) Set the type field (default: "app.log")
WithoutTime() Omit the timestamp
Output

When using WithJSON(), log records look like:

{"level":"INFO","msg":"Lambda Invoked","record":{"functionName":"my-func","version":"$LATEST","requestId":"abc-123"},"type":"app.log"}

When using WithText():

level="INFO" msg="Lambda Invoked" record.functionName="my-func" record.version="$LATEST" record.requestId="abc-123" type="app.log"
Log Levels

The handler maps AWS Lambda log levels to slog.Level values:

Lambda Level slog Level
TRACE slog.LevelDebug - 4
DEBUG slog.LevelDebug
INFO slog.LevelInfo (default)
WARN slog.LevelWarn
ERROR slog.LevelError
FATAL slog.LevelError + 4

Documentation

Overview

Package sloglambda is a slog.Handler for AWS Lambda functions.

It auto-configures log format (JSON or text) and level from Lambda's advanced logging environment variables. Options override env values.

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

Index

Examples

Constants

This section is empty.

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.

Example
package main

import (
	"log/slog"
	"os"

	sloglambda "github.com/maddiesch/slog-lambda"
)

func main() {
	handler := sloglambda.NewHandler(os.Stdout)
	logger := slog.New(handler)

	slog.SetDefault(logger)

	slog.Info("Hello, world!")
}

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(attr []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.

Example
package main

import (
	"log/slog"
	"os"

	sloglambda "github.com/maddiesch/slog-lambda"
)

func main() {
	handler := sloglambda.NewHandler(os.Stdout, sloglambda.WithJSON(), sloglambda.WithoutTime())
	logger := slog.New(handler)

	slog.SetDefault(logger)

	slog.Info("Hello, world!")
}
Output:

{"level":"INFO","msg":"Hello, world!","record":{"functionName":"test-function","version":"$LATEST"},"type":"app.log"}

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.

Example
package main

import (
	"log/slog"
	"os"

	sloglambda "github.com/maddiesch/slog-lambda"
)

func main() {
	handler := sloglambda.NewHandler(os.Stdout, sloglambda.WithText(), sloglambda.WithoutTime())
	logger := slog.New(handler)

	slog.SetDefault(logger)

	slog.Info("Hello, world!")
}
Output:

level="INFO" msg="Hello, world!" record.functionName="test-function" record.version="$LATEST" type="app.log"

func WithType

func WithType(logType string) Option

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

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