Welcome back to 12 Days of DigitalOcean! Over the past five days, you’ve built a fully functional Birthday Reminder Service that stores contacts in a PostgreSQL database, checks for birthdays, sends SMS notifications, runs automatically using DigitalOcean Functions, and automates scheduling with triggers.
Today, we’ll make the app even more reliable by setting up logging. Logs are the unsung heroes of every app—they help you see what’s happening behind the scenes, catch bugs, and troubleshoot issues effectively.
DigitalOcean provides a built-in way to forward all your function logs - both console
outputs and error messages—to external logging services like Papertrail, BetterStack (formerly LogTail), or DataDog. These services offer powerful features, such as:
For example, if your Birthday Reminder Service suddenly stops sending reminders, external logs can help you pinpoint the issue - whether it’s a database connection error, a misconfigured API, or something else entirely.
By the end of this guide, you’ll know how to:
We’ll use Papertrail as our example for this tutorial, but the steps can easily be adapted for other services like BetterStack or DataDog.
Before we get started, make sure you have:
project.yml
file for your function.First, make sure Papertrail is enabled in your account.
Log in to your DigitalOcean account.
Navigate to SaaS Add-ons in your dashboard.
Find Papertrail and click Add Papertrail.
Choose the free plan (great for experimenting, though it doesn’t include advanced log search).
Agree to the terms and click Add Papertrail to enable the service.
That’s it! Papertrail is now connected to your DigitalOcean account. 🎉
Now that Papertrail is enabled for your DigitalOcean account, the next step is to create a log destination in Papertrail that uses token-based authentication.
If you already have a Papertrail account, log in to your account. If not, don’t worry—Papertrail makes it easy to start. You can sign up for free with no credit card required. Once signed up, you’ll be ready to create your log destination.
Log in to your Papertrail account and navigate to Settings > Log Destinations.
Click New Destination.
On the destination creation page:
my-birthday-reminder-service-logs
.Once created, you’ll see a Token under the log destination details. Copy this token—you’ll need it to configure your app.
.env
File and project.yml
To forward logs from your DigitalOcean Function to Papertrail, you’ll need to add the token to your .env
file and reference it directly in the LOG_DESTINATIONS
field in your project.yml
.
Open your .env
file in the root of your project and add the following line:
PAPERTRAIL_API_TOKEN=<your-token>
Replace <your-token>
with the token you copied from Papertrail.
Next, open your project.yml
file and update the environment
section to include the LOG_DESTINATIONS
field. Here’s what your updated project.yml
should look like:
packages:
- name: reminders
shared: false
environment:
DB_NAME: "${DB_NAME}"
DB_USER: "${DB_USER}"
DB_PASSWORD: "${DB_PASSWORD}"
DB_HOST: "${DB_HOST}"
DB_PORT: "${DB_PORT}"
TWILIO_ACCOUNT_SID: "${TWILIO_ACCOUNT_SID}"
TWILIO_AUTH_TOKEN: "${TWILIO_AUTH_TOKEN}"
TWILIO_PHONE_FROM: "${TWILIO_PHONE_FROM}"
TWILIO_PHONE_TO: "${TWILIO_PHONE_TO}"
LOG_DESTINATIONS: '[{"papertrail":{"token":"${PAPERTRAIL_API_TOKEN}"}}]'
functions:
- name: birthdays
runtime: python:default
triggers:
- name: daily-birthday-trigger
sourceType: scheduler
sourceDetails:
cron: "50 7 * * *"
In this configuration:
PAPERTRAIL_API_TOKEN
from the .env
file is automatically referenced in the LOG_DESTINATIONS
field.Save the changes and deploy your function:
doctl serverless deploy my-birthday-reminder-service
Tip: Make sure you’re not inside the my-birthday-reminder-service
directory when you run this command. Run it from the parent directory to avoid deployment errors.
With the configuration complete, let’s make sure everything is working by verifying logs. We’ll start with a quick test using a curl
command, then check if your function sends logs as expected.
Before testing your function, use the following curl
command to ensure your Papertrail log destination is set up correctly:
Open your terminal and run this command:
curl -u :<your-token> \
-H "Content-Type: application/json" \
-d '{"hello": "world"}' \
https://logs.collector.solarwinds.com/v1/log
Replace <your-token>
with the token you copied from Papertrail.
Log in to your Papertrail account and navigate to the Events tab (https://my.papertrailapp.com/events).
Look for a log entry that says {"hello": "world"}
:
curl
command. Ensure you’re using the correct token and endpoint URL.Once you’ve confirmed the log destination works, it’s time to test your function:
doctl serverless activations invoke reminders/birthdays
Tip: If you prefer, you can also manually run the function from the control panel, by navigating to your function, and click Run.
Confirmation if run from the Control Panel:
2024-12-26T22:58:27.164476652Z stdout: Logs will be written to the specified remote location. Only errors in doing so will be surfaced here.
This confirms that logs are being forwarded to Papertrail successfully.
Log in to your Papertrail account and navigate to the Events tab (https://my.papertrailapp.com/events).
Check for log entries from your function, such as:
2024-12-26T23:05:39 INFO: Reminder sent to +1234567890 for Alice Smith's birthday.
If you don’t see any logs in Papertrail, here are some steps to debug:
Verify Configuration: Ensure your project.yml
file includes the LOG_DESTINATIONS
field and correctly references the Papertrail token.
Check for Logging Statements: Make sure your __main__.py
file contains print
or logger.info
statements to generate logs. For example:
import logging
logging.basicConfig(level=logging.INFO)
def main(params):
logging.info("Reminder function triggered.")
print("Reminder function executed.")
Test with Curl: Run the curl
command again to confirm that the token and log destination are still working as expected.
Redeploy the Function: If changes were made to the configuration or the logging statements, redeploy the function:
doctl serverless deploy my-birthday-reminder-service
Check Activation Logs: Use the following command to check the most recent activation logs for errors:
doctl serverless activations logs --function reminders/birthdays --last
By following these steps, you should be able to identify and resolve any issues with your logging setup.
Today, you leveled up your Birthday Reminder Service by:
With logging in place, you can monitor and troubleshoot your app more effectively. 🎉
Let’s now take a moment to celebrate everything we’ve accomplished with the Birthday Reminder Service so far. Over the past six days, you’ve:
Next up, we’re switching gears to build our second app: the Email-Based Receipt Processor. This one’s a little different but equally exciting. Get ready to learn more cool tools and techniques as we dive into the next challenge tomorrow! 🚀
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!