A step-by-step guide to logging Robot Framework RPA events and failures to Bugsnag

Philip Seifi
6 min readAug 15, 2020

The Robot Framework and the RPA tooling the team at Robocorp have built around it are a breath of fresh air in the world of Robotic Process Automation.

Robocode makes it easy to get started even if you have no programming experience, yet unlike visual RPAs from UI Path, Automation Anywhere or Electroneek, it allows experienced programmers to leverage the full power of Python when needed.

Illustration adapted from the original by Robocorp.

One aspect of the system that still requires some work, however, are its logging capabilities.

The Robot Framework was originally built for acceptance testing in software development. Once you try to implement complex process automation that runs unattended on Robocloud, and interacts with sites outside of your control, you’ll soon wish for a more actionable, business-specific paper trail than the default logs.

Below, I will share how to integrate the Robot Framework with Bugsnag as a central repository for both fatal failures, and detailed logs of everything your robot has achieved in each run.

Project set up

The rest of this tutorial will expand on the Web store order robot tutorial by Robocorp, and I will assume you’re starting where that project left off.

The robot in the tutorial above processes web store orders based on an Excel file. Next, you’ll learn how to log the robot’s actions and code errors to a bug tracking system, group them by order, and notify the right person if an order requires human attention.

Connecting to Bugsnag

You will first need to create a new account at Bugsnag.

The reason I chose Bugsnag over Sentry, Loggly, Timber or a different logging system is it allows you to group events together by user-defined categories.

This feature is crucial for RPA logging, because you usually care more about what happened in a particular automation run, rather than a specific function or file (the default grouping in most logging systems).

But more on that later…

Once you have your Bugsnag account ready, navigate to your Project Settings, and get your Notifier API key.

Next, create a new Python file called Logger.py in the libraries folder of your project, with the following code. Make sure to replace API_KEY with the one you got from Bugsnag!

You’ll also need to run pip install bugsnag in your project to get the official Python logging package, and if you’re going to run the project on Robocloud, add bugsnag under to config/conda.yaml:

Preparing the context

Bugsnag allows us to group all our events into any categories that make sense to our business. In our case, the most logical unit is the processed Order.

Before reading on, open devdata/Data.xlsx in your web store tutorial project, and add an ID column with random identifiers for each order. We’ll use these to identify orders in Bugsnag.

You’ll also need to add “id”: row[“ID”], to your libraries/Orders.py script that reads from the spreadsheet:

Finally, we’ll create a Suite variable that will hold the currently processed order’s ID, and make it accessible throughout our system.

You can do this by adding Set Suite Variable ${CURRENT_ORDER_ID} ${order[“id”]} at the beginning of the Process order keyword in resources/keywords.robot:

Logging events

To log events from Robot Framework, we need to define a new keyword in Python that takes some arguments, and CURRENT_ORDER_ID and sends them over to the generic log_to_bugsnag function we defined earlier.

To do that, simply create a MyUtils.py file in your libraries folder with the following code:

This keyword takes a message, severity level, and any number of additional named arguments to pass to Bugsnag as metadata.

It also uses Robot’s BuiltIn().get_variable_value function to get the order ID variable from the Suite scope, and formats it for display as the name of our Bugsnag event group (context).

You can now import the library at the top of your keywords.robot:

And start calling it as you would any other Keyword in the Robot Framework language!

For example, let’s log a few events in the Process order Keyword:

If you now deploy the project to Robocloud and run the process, this is what you should see in your Bugsnag dashboard:

Robot Framework RPA events grouped in Bugsnag

If you click through into one of the groups, you can then explore individuals events in reverse-chronological order, including custom metadata under the Details tab.

RPA event details in Bugsnag

Logging Robot Framework errors

What we have so far works great on successful runs, but if the robot runs into a fatal failure, the log will simply cut off at its last event.

To learn what caused the failure, or even just find out that it happened, you’d still need to go into Robocloud and dig through the log until you find the right run.

To make our logger even better, we’ll intercept Robot Framework’s failure messages and append them to all our other order events.

To do that, create a new RobotLogListener.py file in your libraries folder, with the following script:

You can read more about using Libraries as Listeners in the Robot Framework docs.

Long story short, the Class above registers as a listener to the framework’s log_message event, and passes the error message to Bugsnag using the function we wrote at the very beginning.

Note that the log_message event fires even within Run Keyword And Return Status and Run Keyword And Continue On Failure.

If you’d like to only log Fatail Failures, replace the _log_message method with the following _end_test listener, which only fires at the end of a failed test run:

You’ll need to import the listener inside your project, just as you did with MyUtils:

To test that everything works as expected, let’s introduce a mistake in one of our purchasing keywords.

If you delete your old logs, redeploy the package to Robocloud, and run it, you should start to see errors inside each event group:

Robot Framework exceptions in Bugsnag

Error notifications

Note the red dots on the right. We’ve set the severity of events passed from our listener to the error level. You can use these to filter a view in Bugsnag, or schedule automatic email and Slack notifications.

You can also enable a daily or weekly summary of all runs, something I find very handy, and wish Robocloud offered as a feature without the needs for external tools.

Daily summary email from Bugsnag.

Conclusion

Robocloud is a young and quickly evolving project, and I’m sure more nuanced logging and reporting tools are in the works.

In the meantime, I hope this tutorial helps you set up a system that works for your project.

If you have any feedback, corrections, or if you found an even better logging solution for your Robot Framework RPA, please let me know in the comments!

Liked this post? Follow me on 🐦Twitter @seifip

--

--

Philip Seifi

Founder https://colabra.app | Cross-pollinating between industries and cultures. | Nomad entrepreneur 🌎 designer 🌸 hacker 💻 | https://seifi.co