Create Our First Log (new version)
In this tutorial, we will log a message into a hello.mcap
file.
Setup our tutorial project
In this tutorial we will use uv
.
Let's create a new uv project and add the MCAP Logger package as dependency.
After this we should have the following elements in the project's folder:
We can run our "Hello World" program with the following command:
This should produce the following output on your console:
We will use this "Hello World" program as a base for our tutorial.
Create our first log
First, let's import the logging
package of Python and the McapHandler
class into the hello.py
script.
After we did the import, we will create our Logger
entity with the getLogger
function. We will call the logger
mcap_logger
.
hello.py | |
---|---|
Then we will define the log file we want to create. To do that, we need to import Path
and
specify the log file's name and path. In this case the log file will be called hello.mcap
and we will place it
in the project's directory.
After this, we can create our mcap log handler by initializing it with the path of the log file.
Info
If the parent directory of the defined log doesn't exist, the it will be created.
hello.py | |
---|---|
When we created our handler then we have to add it to the logger. This means that when the logger logs a message then it will hand it to our MCAP handler, which will put this into our log file.
We will set the level of logging to be DEBUG
level both for the logger and for the handler. This basically means
that all log messages will be logged.
We will remove the original print
statement and replace it with an info level log call.
Create the log file
Now that we made modification to the hello.py
, we can run our example again and see the
result.
Note
There will be no console logs, because we only added and McapHandler
to the logger.
Notice that we have a new file in our project directory called hello.mcap
. This file is serialized with
Protobuf, so we can not open it with a text editor and look at the content of it.
To do that we will use Foxglove Studio.
Open our log file
Open Foxglove Studio and use the Open local file...
command to open our hello.mcap
log file. Our log messages is
using Foxglove's Log schema, and we can visualise it
with a Log panel.
We can add a panel following this guide, and after
that we need to configure it to use the /log
topic.
After this we should see our info message on the log panel.
And Voilà! We made our first log with mcap-logger
.
Filtering logs
Notice that in Foxglove Studio, you can filter the logs by their level in the log panel.