sloglambda

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: Unlicense Imports: 14 Imported by: 0

README

slog-lambda

current codecov Doc License

AWS Lambda slog.Handler

package main

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

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

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

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

		return nil
	})
}

Documentation

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
}

func NewHandler

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

NewHandler creates a new Handler that writes log messages to the given io.Writer.

The handler will configure itself using the AWS Lambda advanced logging environment variables: - AWS_LAMBDA_LOG_LEVEL: The log level to use. Valid values are "TRACE", "DEBUG", "INFO", "WARN", "ERROR", and "FATAL". - AWS_LAMBDA_LOG_FORMAT: The log format to use. Valid values are "json" and "text".

See more here: https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs-advanced.html

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!")
}
Output:

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)

func WithJSON

func WithJSON() Option

WithJSON configures the Handler to output log messages in JSON format.

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 configures the log level of the Handler.

The log level determines which log messages will be processed by the Handler.

func WithSource

func WithSource() Option

WithSource configures the Handler to include source code information in log messages.

func WithText

func WithText() Option

WithText configures the Handler to output log messages in text format.

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 configures the Handler's "type" field to the specified value.

func WithoutTime

func WithoutTime() Option

WithoutTime configures the Handler to exclude the time field from log messages.

Jump to

Keyboard shortcuts

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