Tutorial

12 Days of DigitalOcean (Day 6) - Setting Up External Logging for Your Function-Based Birthday Reminder Service

Published on December 26, 2024
12 Days of DigitalOcean (Day 6) - Setting Up External Logging for Your Function-Based Birthday Reminder Service

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.

✨ Why Use External Logging Services?

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:

  • Search and filter logs to find specific events or errors quickly.
  • Keep a history of logs for debugging or audits.
  • Monitor multiple apps in one place by aggregating logs into a centralized dashboard.
  • Set up alerts for critical issues and integrate with tools like Slack or PagerDuty.

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.

🚀 What You’ll Learn

By the end of this guide, you’ll know how to:

  • Set up logging for your DigitalOcean Function using Papertrail.
  • Create a log destination in Papertrail, which is the entry point for forwarding your logs securely using token-based authentication.
  • Verify that your logs are being captured in Papertrail’s dashboard.

We’ll use Papertrail as our example for this tutorial, but the steps can easily be adapted for other services like BetterStack or DataDog.

🛠 What You’ll Need

Before we get started, make sure you have:

  • Your Birthday Reminder Service deployed on DigitalOcean (from Day 4).
  • Access to your Papertrail account (you’ll create one if you don’t have it yet).
  • The project.yml file for your function.

🧑‍🍳 Recipe for Day 6: Setting Up Logging

Step 1: Enable Papertrail Logging in DigitalOcean

First, make sure Papertrail is enabled in your account.

  1. Log in to your DigitalOcean account.

  2. Navigate to SaaS Add-ons in your dashboard.

  3. Find Papertrail and click Add Papertrail.
    Papertrail in DigitalOcean SaaS Add-ons

  4. Choose the free plan (great for experimenting, though it doesn’t include advanced log search).

  5. Agree to the terms and click Add Papertrail to enable the service.

Choose a Papertrail Plan

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.

Step 2: Create a Log Destination in Papertrail with 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.

  1. Log in to your Papertrail account and navigate to Settings > Log Destinations.

  2. Click New Destination.

    Papertrail Log Destinations Setup

  3. On the destination creation page:

    • Name the destination: Use something descriptive, like my-birthday-reminder-service-logs.
    • Under Accept connections via, choose Token for token-based authentication (make sure HTTPS is selected).
    • Click Create.

    Papertrail Destination Settings

  4. Once created, you’ll see a Token under the log destination details. Copy this token—you’ll need it to configure your app.

Papertrail System Settings

Step 3: Update Your .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.

  1. 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.

  2. 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:

    • The PAPERTRAIL_API_TOKEN from the .env file is automatically referenced in the LOG_DESTINATIONS field.
    • This ensures the token is securely passed to Papertrail for authentication.
  3. Save the changes and deploy your function:

    doctl serverless deploy my-birthday-reminder-service
    

serverless_deploy_birthday_reminder

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.

Step 4: Verify Logs in Papertrail

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.

Quick Test with Curl

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.

terminal_curl_papertrail_test

  • 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"}:

    papertail_dashboard_oldest_event

    1. If the log appears: Congratulations! Your log destination is set up correctly.
    2. If not: Double-check the token and retry the curl command. Ensure you’re using the correct token and endpoint URL.

Test Your Function

Once you’ve confirmed the log destination works, it’s time to test your function:

  • Trigger your function manually to generate logs:
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:

    • After clicking Run, you’ll see a confirmation that the function was activated.
    • If everything is set up correctly, you’ll also see a message similar to this in the logs:
    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.
    

    reminders_birthdays_source_run

    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).

    papertrail_events_log

  • Check for log entries from your function, such as:

    2024-12-26T23:05:39 INFO: Reminder sent to +1234567890 for Alice Smith's birthday.
    
    1. If the logs appear: Your function is successfully sending logs to Papertrail.
    2. If not: Proceed to the troubleshooting section below.

Troubleshooting

If you don’t see any logs in Papertrail, here are some steps to debug:

  1. Verify Configuration: Ensure your project.yml file includes the LOG_DESTINATIONS field and correctly references the Papertrail token.

  2. 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.")
    
  3. Test with Curl: Run the curl command again to confirm that the token and log destination are still working as expected.

  4. Redeploy the Function: If changes were made to the configuration or the logging statements, redeploy the function:

    doctl serverless deploy my-birthday-reminder-service
    
  5. 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.

🎁 Wrap-Up

Today, you leveled up your Birthday Reminder Service by:

  1. Enabling Papertrail logging in DigitalOcean.
  2. Created a token-based log destination in Papertrail.
  3. Forwarding logs from your app to Papertrail by using Papertrail’s token based log destination.
  4. Verified logs in Papertrail.

With logging in place, you can monitor and troubleshoot your app more effectively. 🎉

Looking Back: The Journey So Far

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:

  1. Day 1: Set up a PostgreSQL database to store contacts
  2. Day 2: Built a Python script to connect to the database and fetch birthdays
  3. Day 3: Added functionality to send SMS reminders using Twilio
  4. Day 4: Deployed the app to DigitalOcean Functions to make it serverless and scalable
  5. Day 5: Automated reminders to run on a schedule
  6. Day 6: Set up logging to capture and save logs using services like PaperTrail

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.

Learn more about our products

About the authors

Default avatar

Sr Technical Writer

Senior Technical Writer @ DigitalOcean | 2x Medium Top Writers | 2 Million+ monthly views & 34K Subscribers | Ex Cloud Consultant @ AMEX | Ex SRE(DevOps) @ NUTANIX


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.