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 ¶
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!")
}
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 ¶
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 WithoutTime ¶
func WithoutTime() Option
WithoutTime omits the timestamp from log output. Useful for testing or when CloudWatch already provides timestamps.