Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: 📝 add sections about log level #5

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ The handler will log the following attributes by default:
* `request-id`: the unique request ID for the lambda function invocation.
* `function-arn`: the Amazon Resource Name (ARN) that's used to invoke the function, indicates if the invoker specified a version number or alias.

Also, it will set the log level based on the `LOG_LEVEL` environment variable.

### Using the handler as default handler

```go
Expand Down Expand Up @@ -98,6 +100,48 @@ slog.SetDefault(slog.New(slogawslambda.NewAWSLambdaHandler(ctx, &slog.HandlerOpt
})))
```

### Setting the log level using CloudFormation/SAM

Another important aspect is to customize the log level.
Typically you might want to have a higher level in production (e.g. only log error level messages) to lower the cost of your CloudWatch logs.
This can be achieved by providing a value for the environment variable `LOG_LEVEL`.

If you are using CloudFormation or SAM as your infrastructure as code tool you could do the following:

```yml
BudgetCategorizerFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: bootstrap
Environment:
Variables:
LOG_LEVEL: 'debug'
```

### Setting the log level using Terraform

If you are using Terraform as your infrastructure as code tool you could do the following:

```terraform
resource "aws_lambda_function" "hello_world_arm64" {
filename = "bootstrap.zip"
function_name = "helloWorldarm64"
architectures = ["arm64"]
role = aws_iam_role.lambda_role.arn
handler = "bootstrap"
runtime = "provided.al2"
source_code_hash = filebase64sha256("bootstrap.zip")

environment {
variables = {
LOG_LEVEL = "debug"
}
}
}
```


## 📓 Why should we want to log request ID and function ARN

Embracing serverless comes with scalability and resilience benefits, but logging across a distributed system can be challenging.
Expand Down
Loading