Structured logging with Robot Framework and structlog
By default, Robot Framework captures all console output (stdout) and includes it in its log file. This means that simple logging statements or even print()
calls will appear in the Robot Framework logs automatically.
Structured logging
But I like to use more advanced structured logging via a library called structlog. It allows storing custom key-value pairs and additional metadata next to the basic log event message.
I had some problems setting this up. So here are some notes in you case you plan to do something similar:
My solution involves a custom structlog processor which send log events directly to Robot Frameworks. Structlog itself will log to the stderr console output so logs do not get captured twice. This gives you full control over which details Robot Framework receives, while the console output remains independently configurable.
You can find the complete code in my git repository python-ideas (links to fixed commit). Below I will explain the relevant parts.
Configure structlog
In the past I created a custom processor for structlog which adds information about file and line number to the console output. This way you can click on the log output and jump to the exact location in your code editor.
Now I extended this configuration with a new processor for Robot Framework:
|
|
As you can see structlog will invoke the robot framework logger. Additional key-value pairs of the structured log will be attached as json encoded string. At the end structlog will write its own output to stderr.
Structlog listener for Robot Framework
A listener can add new custom functionality to Robot Framework. The following listener runs the our previous configuration code for structlog on its initialization. It sets the argument robotframework_logger=True
. That’s all! 😁
|
|
Use listener on test execution
Now you have to execute a test case while the StructlogListener
is enabled. There are several ways to add a listener. It depends on how you run robot framework test cases.
robot cli command with listener argument
Example 1: with default arguments for structlog listener:
robot \
--console "dotted" \
--outputdir "output" \
--listener "robotframework_tips.utils.robot_listeners.StructlogListener" \
"robotframework_tips/examples/failing_test_with_exception/Failing Test.robot"
Example 2: manual arguments for structlog listener and an additional Exception Traceback Listener:
robot \
--console "dotted" \
--outputdir "output" \
--listener "robotframework_tips.utils.robot_listeners.ExceptionTracebackListener;ERROR" \
--listener "robotframework_tips.utils.robot_listeners.StructlogListener;true;false" \
"robotframework_tips/examples/failing_test_with_exception/Failing Test.robot"
RobotCode (vscode extension)
- RobotCode is an extension for vscode
- Install this extension from the vscode marketplace
- then you can configure custom settings like listener via file called
robot.toml
at the root of your opened folder in vscode (mostly the root of your git repo)
|
|
Open Robot Framework logs
Check the Robot Framework log.html
file.
It will contain your structlog logs with correct log levels and json encoded key-value pairs.