0
0
Supabasecloud~30 mins

Monitoring and logging in Supabase - Mini Project: Build & Apply

Choose your learning style9 modes available
Monitoring and Logging with Supabase
📖 Scenario: You are managing a small web app that uses Supabase as its backend. You want to keep track of user activity and errors by setting up simple monitoring and logging. This will help you understand how users interact with your app and quickly find problems.
🎯 Goal: Build a basic monitoring and logging setup using Supabase. You will create a table to store logs, add a configuration for log levels, write a function to insert logs based on the level, and finally display the logs.
📋 What You'll Learn
Create a Supabase table called logs with columns id (auto-increment), level (text), message (text), and timestamp (timestamp with default current time).
Add a variable log_level to control which logs to record (e.g., 'INFO', 'ERROR').
Write a function add_log(level, message) that inserts a log into the logs table only if the log's level is equal or higher priority than log_level.
Query and print all logs from the logs table ordered by timestamp.
💡 Why This Matters
🌍 Real World
Monitoring and logging help developers track app behavior and quickly find issues. Supabase makes it easy to store and query logs in a database.
💼 Career
Understanding how to set up logging and monitoring is essential for DevOps roles and backend development to maintain reliable applications.
Progress0 / 4 steps
1
Create the logs table in Supabase
Write the SQL command to create a table called logs with columns: id as an auto-incrementing primary key, level as text, message as text, and timestamp as a timestamp with default value as the current time.
Supabase
Hint

Use SERIAL PRIMARY KEY for auto-increment id. Use CURRENT_TIMESTAMP as default for timestamp.

2
Add a log_level configuration variable
In your Supabase client code, create a variable called log_level and set it to the string 'INFO' to control the minimum level of logs to record.
Supabase
Hint

Just assign the string 'INFO' to a variable named log_level.

3
Write the add_log(level, message) function
Write a function called add_log that takes level and message as parameters. It should insert a new row into the logs table only if the level is equal or higher priority than log_level. Use the priority order: ERROR > WARN > INFO. Use Supabase client methods to insert the log.
Supabase
Hint

Use a dictionary to map log levels to numbers for comparison. Insert only if the level is high enough.

4
Query and print all logs ordered by timestamp
Write code to query all rows from the logs table ordered by timestamp ascending. Then print the list of logs.
Supabase
Hint

Use supabase.table('logs').select('*').order('timestamp', desc=False).execute() to get logs. Print the data attribute.