AWS Lambda supports advanced logging controls[1] that help you manage how your function's logs are captured, processed, and consumed.
- includes a shortcut
LambdaLogger()
for setting JSON log format in slog. - overrides the name of the time key to
timestamp
(note: not necessary for Lambda). - set the log-level in your application to match the setting in Lambda (note: Lambda will filter logs according using the
level
field in each log and the log-level you set in Lambda function config any way) - provides a
FatalError(...)
function that willslog.Error(...)
and thenos.Exit(1)
package main
import (
"github.com/brightbock/slogcloud"
. "github.com/brightbock/slogcloud/helpers"
"log/slog"
)
func init() {
slog.SetDefault(slogcloud.LambdaLogger())
}
func main() {
slog.Warn("This is a warning!")
FatalError("log and exit with status 1")
}
Output:
{"timestamp":"2023-01-01T01:01:01.000000+00:00","level":"WARN","msg":"This is a warning!"}
{"timestamp":"2023-01-01T01:01:01.000000+00:00","level":"ERROR","msg":"log and exit with status 1"}