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.
Fields are written in a fixed order, matching slog.JSONHandler and slog.TextHandler: "time", "level", "source", "msg", then the handler's "type" and "record", then attrs and groups in the order they were added. Values that cannot be encoded as JSON are written as "!ERROR:<reason>" strings so a record is never dropped.
The handler reserves two top-level keys: "record" (Lambda function name, version, and request ID) and "type" (see WithType). When the handler sets one of these and an attr or group uses the same key at the top level, the handler's value wins and the caller's value moves to the first unused key formed by appending underscores ("record_", "type_", "type__", ...).
The rename is best-effort and has a few edge cases:
- Two attrs with the same reserved key in one record both move to the same fallback key, so the output contains that key twice. This matches slog.JSONHandler, which also writes duplicate keys as given.
- A top-level attr whose value is a slog.LogValuer counts as taken even if it resolves to an empty group and is never written, so the fallback may skip a key that ends up unused.
- In text output, groups flatten into dotted keys, so a user key that already contains a dot ("record.requestId") is not renamed and lands in the same namespace as the handler's fields.
- "time", "level", "msg", and "source" are not reserved. A user attr with one of those keys is written in addition to the built-in field, as slog.JSONHandler does.
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), AWS_LAMBDA_LOG_FORMAT (json, text), AWS_LAMBDA_FUNCTION_NAME, and AWS_LAMBDA_FUNCTION_VERSION from the environment once, at construction. Any provided options take precedence over environment values.
Example ¶
package main
import (
"log/slog"
"os"
sloglambda "github.com/angryboat/slog-lambda"
)
func main() {
handler := sloglambda.NewHandler(os.Stdout)
logger := slog.New(handler)
slog.SetDefault(logger)
slog.Info("Hello, world!")
}
Output:
type Option ¶
type Option func(*Handler)
Option configures a Handler. Pass options to NewHandler.
func WithFunctionName ¶
WithFunctionName sets the function name included in the "record" field. Defaults to the value of AWS_LAMBDA_FUNCTION_NAME. An empty name omits it.
func WithFunctionVersion ¶
WithFunctionVersion sets the function version included in the "record" field. Defaults to the value of AWS_LAMBDA_FUNCTION_VERSION. An empty version omits it.
func WithJSON ¶
func WithJSON() Option
WithJSON sets the output format to JSON.
Example ¶
package main
import (
"log/slog"
"os"
sloglambda "github.com/angryboat/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!","type":"app.log","record":{"functionName":"test-function","version":"$LATEST"}}
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. A nil level means INFO, matching slog.HandlerOptions.
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/angryboat/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!" type="app.log" record.functionName="test-function" record.version="$LATEST"
func WithoutTime ¶
func WithoutTime() Option
WithoutTime omits the timestamp from log output. Useful for testing or when CloudWatch already provides timestamps.