This Tutorial includes a follow along video, on Desktop you can click "Play and Follow Video" to be guided through the article as the video progresses.
To start logging, you will need a project to log against, this tutorial will cover logging a basic slspress application in an development environment using the serverless offline plugin.
We will then deploy the application, and view online logs.
Once you have your serverless project created, and have installed the serverless offline plugin, you can start your service using the command:
serverless offline
Once this has launched, you should be presented with a summary of endpoints and shown what port your service is running on (the default is http://localhost:3000
).
Once you hit your endpoint with the relevant request type, you will be presented with logs that show the request information and a stringified version of your response body.
There are many methods for viewing cloudwatch logs, the ones we have covered here are; using Dumptruck, Serverless CLI and the AWS Cloudwatch web console.
If it takes a while for events to show in Cloudwatch (or CLI tool) - don't worry, this is normal. You usually won't see the full event information show up in Cloudwatch until the event completion has been given some time process.
If you plan on logging any debug payloads, ensure you stringify any JSON payloads otherwise you will see
[object object]
Dumptruck is one of the utilities we've created for log management. For more information, please check the following link.
If you don't already have it installed you can run npm install -g dumptruck
to install it.
Dumptruck will scan your AWS CLI config, and any local serverless project to pick the best config for collecting logs, and can be run on any machine with the correct AWS credentials and does not have to in the context of a Serverless project.
To run dumptruck simply invoke it with the following command.
dumptruck
This will guide you through choosing an AWS profile, Region and then Lambda. These are pre selected by most recently used, and Lambdas are sorted by most recently updated.
Once connected it will poll the Cloudwatch log group and bring in the events as they come in.
Once you close the log stream (ctrl+C
) you will be given the arguments to allow you to directly invoke the same log tail in the future, e.g:
dumptruck -p "default" -r "eu-west-1" -l "lamdba-name"
When in a serverless project folder, you can use the Serverless CLI to call the logs for your existing project.
serverless logs -f <func-name>
Where the <func-name>
is the name of the Lambda function you are looking for.
Additionally, you can use the --tail
flag (or -t
for short) to stream the logs automatically to your console.
serverless logs --function <func-name> --tail
## or
serverless logs -f <func-name> -t
We recommend you doing that on a different terminal window to keep an 'auto-refreshed' log. Full documentation can be found on the serverless website.
One thing to keep in mind when using the Serverless CLI logging tool, is that you need to be in a serverless project folder to run it.
When you open a log stream, these logs are shown in the row view and you may find it easier to read these by clicking on the "text" checkbox to show all event data on a single view.
If you have a very active log stream, the events may not always populate into the expected log streams seen in step 3 above, so it's always a good idea to use the "Search Log Group" option than apply the required filters.
If you want bind actions to these Cloudwatch events you trigger a Lambda from Cloudwatch, more information is in our Listening to CloudWatch events tutorial with examples.