Zeinab Abbasimazar
Zeinab Abbasimazar

Reputation: 10469

How can I control log level of plain-text debug files in robot framework?

I have read this documentation which explains how to create plain-text log files during execution. As it says it is a Debug File and All messages got from test libraries are written to them; but I'm looking for a way to control the log level of its content. Isn't there any mechanism or trick to obtain that? Or even any other robot framework option which also have the functionality of Debug file (writing test execution messages to a plain-text while test is in progress)?

Upvotes: 2

Views: 2156

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

There is no way to control the amount of detail that goes into the debug file. The whole point of the debug file is to create a detailed output for debugging purposes.

If you want information to be written to a plain text file as the test is running, you can create a listener that writes whatever you want, whenever you want.

For example, lets say you want to write the time that each test starts and ends. The first step would be to create a listener. For this example, create a file named "CustomLog.py" with the following contents:

import datetime
class CustomLog:
    ROBOT_LISTENER_API_VERSION = 2

    def __init__(self, filename='listen.txt'):
        self.logfile = open("/tmp/robot.log", 'w')

    def _write(self, message):
        now = datetime.datetime.now()
        self.logfile.write(str(now) + " " + message + "\n")
        self.logfile.flush()

    def start_test(self, name, attrs):
        self._write("start_test: %s" % name)

    def end_test(self, name, attrs):
        self._write("end_test: %s (%s)" % (name, attrs["status"]))

    def close(self):
        self.logfile.close()

You can pass the name of this file to robot via the --listener argument:

robot --listener CustomLog.py ...

The listener interface gives you the ability to perform actions which suites, tests, and keywords start and stop. These methods are all covered in the documentation.

Upvotes: 3

Related Questions