Python Automation

Logs in Python for RPA automation [+example]

Logs are crucial, as they help manage the application, allowing us to gain insights into its behavior, errors, exceptions, and tasks performed. In the context of RPA automation, logs are essential to help with transparency and support efficient troubleshooting. In this article, we will explore the use of logs in RPA automation, highlighting the importance of their implementation and examples of how to do this with Python, a programming language of great importance in this development area.

Logs in Python

Logs in Python can be essential support when developing your RPA automation, as the Python language can be an intelligent choice due to the popularity and simplicity of the programming language, in addition to the vast support and availability of libraries and frameworks that can make the development process much easier.

Learn more about the advantages of Python for RPA: Python RPA vs Low-code comparison.

Logs can also be a chronological record of actions and events in automation. This same log can be of different types and even return information necessary to understand what happened at that particular moment and why.

Types of Logs in RPA

Logs in RPA may vary, depending on the reality of the business. That’s why it’s important to understand what types of logs your team needs to consider for automation:

  • Execution logs: These logs refer to what is running in the code developed for your RPA project. These could be, for example, “exceptions,” which are technical errors that occurred;
  • Security logs: These logs have a more specific context about the security of your automation, as it is also a software project, and it is essential to consider the quality and security of the code and information transmitted at each stage of execution;
  • Logs relacionados ao negócio: These logs have the context of the process that the bot is executing, containing information such as which task was performed, which stage the bot is in at a given moment, what the data is, etc.;
  • Among others.

Python log structure for RPA automation

The structure of these logs may vary according to the business’s needs and requirements. However, it is essential to have at least the date and time of the event, the message corresponding to the event, and some identification related to the context, among others.

How to develop and consult logs in RPA?

Now, let’s go to hands-on! In the following topics, we will build an example of how to develop your logs using BotCity’s Python framework and make your logs available to be monitored by your team and management using BotCity Orchestrator.

In Orchestrator, you can create logs independently of other entities in the automation architecture. And to connect it to our robot, we can use the BotCity Maestro SDK.

Example of a bot to develop our logs

For example, let’s use a robot developed in Selenium and Python that logs in to the Practice Test Automation. The robot should open the page, log in, confirm that the login happened, and then log out and confirm that it worked. If you want to follow the same example, create a folder on your computer and, inside it, add:

  • a file called requirements.txt;
  • a file called bot.py;
  • the web driver according to your browser.

File requirements.txt with dependencies and libraries:

selenium==4.10.0 
webdriver-manager==3.8.5

File bot.py with the main code:

from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service


# using as constants for example only 
# in a real scenario, this should be passed as parameters or using credentials feature
USERNAME = "student"
PASSWORD = "Password123"

def main():
    try:
        # set WebDriver path
        service = Service(executable_path=r"geckodriver.exe")

        # create a bot instance
        bot = webdriver.Firefox(service=service)

        # access the page Practice Test Automation
        bot.get("https://practicetestautomation.com/practice-test-login/")

        # search for the element input to add username
        input_username = bot.find_element(By.ID, "username")

        # add the username
        input_username.send_keys(USERNAME)

        # search for the password element
        input_password = bot.find_element(By.ID, "password")

        # add the password
        input_password.send_keys(PASSWORD)

        # search for the button element
        input_button = bot.find_element(By.ID, "submit")

        # click on the button
        input_button.click()

        # wait 3 seconds to ensure that the page opened
        sleep(3)

        # search for the login confirmation
        logged = bot.find_element(By.CSS_SELECTOR, ".post-title")

        # print confirmation text
        print(logged.text)        

        # search for the logout button element
        logout = bot.find_element(By.CSS_SELECTOR, ".wp-block-button__link")

        # click on the button
        logout.click()

        # search for the login title to ensure that the logout finished
        bot.find_element(By.CSS_SELECTOR, "#login > h2:nth-child(1)")

    except Exception as ex:
        # search for the element with the error message
        error_alert = bot.find_element(By.ID, "error")

        # print the error message and stacktrace
        print(error_alert.text)
        print(ex)

    finally:
        # close the browser
        bot.quit()

        # print the message
        print("Finally")

if __name__ == "__main__":
    main()

Before running the code, remember that you must install the dependencies with the following command in your terminal inside the project folder:

python -m pip install --upgrade -r requirements.txt

As indicated in the code comments, follow the instructions in the credentials documentation to have a protected username and password for your automation.

How to add the BotCity framework to the Selenium project

You can install it using the following command:

pip install botcity-maestro-sdk

We will add the library in the dependencies file, requirements.txt: botcity-maestro-sdk. In the bot.py file, we will add the following import:

from botcity.maestro import *

In the main code, from the bot.py file, we will create the BotCity Maestro SDK instance:

def main():
    # instantiating the Maestro SDK
    maestro = BotMaestroSDK.from_sys_args()
    execution = maestro.get_execution()

How to create a log

Let’s start this step by creating the log in the BotCity Orchestrator. In the menu on the screen’s left side, identify the “Automation Control” category. There, we will have the “Execution Log” option.

The BotCity Orchestrator home screen, with the highlighted left side menu, shows the

When entering the “Execution log” option, click on “new log”:

Screenshot focusing on the Execution Log screen, pointing on the right side to the

After that, we will fill in the requested information and add the necessary columns for our project. At the end, click on the “create log” button. In this case, we add the “username” column with the name “User” and a length of 200 characters and the “message” column with the name “Message” and a length of 500 characters. Both columns should be visible.

Log creation screen requesting the information described in the above section of the text.

The “Date” column is automatically created, there is no need to add it. The log will now be available to repositories that have access to it:

The log list screen shows that our

Find out more: log documentation.

Now that we have the log created in Orchestrator, we need to connect it to our code, which will record the logs there. So, in our main code, let’s add the code that creates the log.

But before that, let’s also configure the completion of the task so that when we execute it by the Orchestrator, it can understand whether the task ended successfully or with an error.

Inside the try and except code snippet, let’s add:

try:

    finish_status = AutomationTaskFinishStatus.SUCCESS
    finish_message = f'Task with username "{USERNAME}" executed with success'

except Exception as ex:

    finish_status = AutomationTaskFinishStatus.FAILED
    finish_message = f'Task with username "{USERNAME}" failed: {error_alert.text}'

In the code above, we are reporting the task status according to the bot’s execution and a message so that it is possible to understand what happened.

In the finally code, let’s add:

finally:

        # finish the task
        maestro.finish_task(
            task_id=execution.task_id,
            status=finish_status,
            message=finish_message
        )

        # create the log
        maestro.new_log_entry(
            activity_label="login_control",
            values = {
                "username": USERNAME,
                "message": finish_message
            } 
        )

How to monitor logs in BotCity Orchestrator

To run the robot locally, you need to connect to the Orchestrator. To do that, you must first add this code to log in to the tool:

maestro.login(
        server="type your server here", 
        login="type your login here",
        key="type your key here"
    )

To find out your login and key, you must access the “Dev Environment” area through the menu on the left side of your Orchestrator. Find out more: Dev Environment documentation.

⚠️Remember that you will use this code snippet only while running the bot on your computer. You must remove this code before deploying your bot in Orchestrator to run automatically.

But we can also run it inside the Orchestrator. o do this, the first step will be to deploy your automation following the guidelines in the tutorial for orchestrating your automation.

After deployment, create a test task. We’ll create a test task because, if necessary, we can rerun the task without any problems.

To create the task, click on the “New Task” option in the menu on the left side of Orchestrator and choose your automation. In our example, we call it bot-login. When you identify it, click the “New Task” button at the end of the line.

Leave the box called “Test Task” selected and click the “Create” button:

Task creation screen with the form to fill out. Highlighted showing the

Note: Remember to leave the runner linked during the deployment process available so that it can run the task. More information is in the guide on how to manage the execution of your automation.

After executing the task, you can go to the “Execution Log” screen, identify the log created, and check if it was filled in correctly. Pay attention to the period filter to ensure the log you created will appear.

Log screen created

How to download logs

To download the logs, click the “Export” button above the log and choose one of the available formats: JSON, CSV, or Excel.

 Screenshot of the screen focusing on the created log and the top buttons. The Export button has its menu open, showing the JSON, CSV, and Excel options.

Other actions with logs

To edit the logs, delete them, or even change the repository, click the “Actions” button above the log and choose one of the options.

Screenshot of the screen focusing on the created log and the top buttons. The Actions button has its menu open, showing the options edit, delete, and move to repository.

⚠️ If you delete the log, this operation will erase the entire history and cannot be reversed.

Complete project code

from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service

from botcity.maestro import *

# using as constants for example only 
# in a real scenario, this should be passed as parameters or using credentials feature
USERNAME = "student"
PASSWORD = "Password123"

def main():

    # instantiating the Maestro SDK
    maestro = BotMaestroSDK.from_sys_args()
    execution = maestro.get_execution()

    try:
        # set WebDriver path
        service = Service(executable_path=r"geckodriver.exe")

        # create a bot instance
        bot = webdriver.Firefox(service=service)

        # access the page Practice Test Automation
        bot.get("https://practicetestautomation.com/practice-test-login/")

        # search for the element input to add username
        input_username = bot.find_element(By.ID, "username")

        # add the username
        input_username.send_keys(USERNAME)

        # search for the password element
        input_password = bot.find_element(By.ID, "password")

        # add the password
        input_password.send_keys(PASSWORD)

        # search for the button element
        input_button = bot.find_element(By.ID, "submit")

        # click on the button
        input_button.click()

        # wait 3 seconds to ensure that the page opened
        sleep(3)

        # search for the login confirmation
        logged = bot.find_element(By.CSS_SELECTOR, ".post-title")

        # print confirmation text
        print(logged.text)        

        # search for the logout button element
        logout = bot.find_element(By.CSS_SELECTOR, ".wp-block-button__link")

        # click on the button
        logout.click()

        # search for the login title to ensure that the logout finished
        bot.find_element(By.CSS_SELECTOR, "#login > h2:nth-child(1)")

        finish_status = AutomationTaskFinishStatus.SUCCESS
        finish_message = f'Task with username "{USERNAME}" executed with success'

    except Exception as ex:
        # search for the element with the error message
        error_alert = bot.find_element(By.ID, "error")

        # print the error message and stacktrace
        print(error_alert.text)
        print(ex)

        finish_status = AutomationTaskFinishStatus.FAILED
        finish_message = f'Task with username "{USERNAME}" failed: {error_alert.text}'

    finally:
        # close the browser
        bot.quit()

        # print the message
        print("Finally")

        # finish the task
        maestro.finish_task(
            task_id=execution.task_id,
            status=finish_status,
            message=finish_message
        )

        # create the log
        maestro.new_log_entry(
            activity_label="login_control",
            values = {
                "username": USERNAME,
                "message": finish_message
            } 
        )

if __name__ == "__main__":
    main()

Do you want to start managing your automation better using logs?

We invite you to join our global RPA community to go deeper into this topic. Just log into our Slack or get in touch via our forum. If you have any questions, just call us! And remember to create your free account to test the features we are exploring in our content. When creating your automation to learn how to use the tool, remember to share with the community via Bot Repository.

BotCity Cofounder and CEO

Leave a Reply

Discover more from Blog BotCity - Content for Automation and Governance

Subscribe now to keep reading and get access to the full archive.

Continue reading