Documentation
¶
Overview ¶
Package sloglambda provides a structured logger for AWS Lambda functions using the slog package. It supports both JSON and text log formats and allows for configurable log levels. All configuration is done using the AWS Lambda advanced logging environment variables. See https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs-advanced.html for more information.
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 ¶
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!")
}
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 ¶
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 WithoutTime ¶
func WithoutTime() Option
WithoutTime configures the Handler to exclude the time field from log messages.