Using the log Tag for Debugging
When building applications on platformOS, effective logging and error handling are important for quickly spotting and fixing issues. Seeing what your application is doing and catching errors as they occur can save you both time and frustration.
Before continuing with the Contact Us form, please read about Error Handling and Troubleshooting in the Getting Started section!
In this part of the tutorial, we’ll dive into using the log tag, a simple yet powerful tool for debugging your platformOS application. By adding logging to your workflow, you’ll get valuable insights into how your application is performing and be able to diagnose problems more easily.
Here’s how to implement logging in your app/lib/commands/contacts/create.liquid
file:
app/lib/commands/contacts/create.liquid
{% function contact = 'commands/contacts/create/build', object: object %}
{% log contact, type: 'result of invoking build' %}
{% return contact %}
Explanation:
Variable | Description |
---|---|
contact |
This is the variable containing the processed object. It includes the email and body parameters, with email being downcased. |
type: 'result of invoking build' |
This provides a label for the log entry, making it easier to identify in the logs. |
Accessing logs
To view your logs, you need to use the pos-cli GUI.
To start the GUI, in your command line, run:
pos-cli gui serve --sync staging
Note
Replace staging
with the environment name you want to develop on. To list all available environments use pos-cli env list
. Refer to the platformOS documentation for detailed instructions on starting the GUI.
After starting the GUI, you will see output similar to:
[07:23:19] Connected to https://contactus.staging.oregon.platform-os.com/
[07:23:19] Admin: http://localhost:3333
[07:23:19] ---
[07:23:19] GraphiQL IDE: http://localhost:3333/gui/graphql
[07:23:19] Liquid evaluator: http://localhost:3333/gui/liquid
[07:23:19] Instance Logs: http://localhost:3333/logs
[07:23:20] [Sync] Synchronizing changes to: https://contactus.staging.oregon.platform-os.com/
Open your browser and go to http://localhost:3333/logs to view the logs:
Alternatively, you can use the CLI to render the logs by running the following command:
pos-cli logs staging
Example of introducing a syntax error
Using the log
tag effectively helps in diagnosing problems and ensuring your application runs smoothly.
If you encounter a syntax error, such as an unnecessary comma, you can debug it by viewing the logs.
For testing purposes, let's add an extra comma after "body"
in the app/lib/commands/contacts/create/build.liquid
file:
app/lib/commands/contacts/create/build.liquid
{% parse_json contact %}
{
"email": {{ object.email | downcase | json }},
"body": {{ object.body | json }},
}
{% endparse_json %}
{% liquid
return contact
%}
Now, with this unnecessary comma added, submit the form with test data.
When you introduce an error like this and then access the logs at http://localhost:3333/logs, you will see a Liquid error indicating the issue. This helps you identify and correct syntax errors.
Now, remove the extra comma and continue our task.
app/lib/commands/contacts/create/build.liquid
{% parse_json contact %}
{
"email": {{ object.email | downcase | json }},
"body": {{ object.body | json }}
}
{% endparse_json %}
{% liquid
return contact
%}
Display Data
Now, if you fill in the form and there are no errors in the logs, you can see that nothing is rendered. This is expected as we haven't instructed the system to display anything yet.
In platformOS, you can display data using double curly brackets {{ }}
for output, or dedicated tags like {% print %}
and {% echo %}
.
- Double Curly Brackets
{{ }}
: Used for rendering variables directly in your templates. {% print %}
: Outputs content to the template and is often used for debugging. Read our documentation about print.{% echo %}
: Similar toprint
, but with subtle differences in context handling. Read our documentation about echo.