Souparno
Souparno

Reputation: 360

Truncating logging of Post Request in RobotFramework

I am using the Requests library of robot framework to upload files to a server. The file RequestsKeywords.py has a line

    logger.info('Post Request using : alias=%s, uri=%s, data=%s, headers=%s, files=%s, allow_redirects=%s '
                % (alias, uri, dataStr, headers, files, redir))

This prints out the whole contents of my upload file inside the request in my log file. Now i could get rid of this log by changing the log level however, my goal is to be able to see the log but just truncate it to 80 characters, so I am not browsing through lines of hex values. Any idea how this could be done?

Upvotes: 4

Views: 1525

Answers (3)

Souparno
Souparno

Reputation: 360

If you look at the method in RequestKeywords library, its only calling self. _body_request() at the end. What we ended up doing is writing another keyword that was identical to the original except the part where it called logger.info(). We modified it to log files=%.80s which truncated the file to 80 chars.

def post_request_truncated_logs(
        self,
        alias,
        uri,
        data=None,
        params=None,
        headers=None,
        files=None,
        allow_redirects=None,
        timeout=None):

    session = self._cache.switch(alias)
    if not files:
        data = self._format_data_according_to_header(session, data, headers)
    redir = True if allow_redirects is None else allow_redirects

    response = self._body_request(
        "post",
        session,
        uri,
        data,
        params,
        files,
        headers,
        redir,
        timeout)
    dataStr = self._format_data_to_log_string_according_to_header(data, headers)
    logger.info('Post Request using : alias=%s, uri=%s, data=%s, headers=%s, files=%.80s, allow_redirects=%s '
                % (alias, uri, dataStr, headers, files, redir))

Upvotes: 0

Todor Minakov
Todor Minakov

Reputation: 20077

A solution would be to create a wrapper method, that'll temporary disable the logging, and enable it back once completed.

The flow is - get an instance of the RequestsLibrary, call RF's Set Log Level with argument "ERROR" (so at least an error gets through, if needed), call the original keyword, set the log level back to what it was, and return the result.

Here's how it looks like in python:

from robot.libraries.BuiltIn import BuiltIn


def post_request_no_log(*args, **kwargs):
    req_lib = BuiltIn().get_library_instance('RequestsLibrary')
    current_level = BuiltIn().set_log_level('ERROR')
    try:
        result = req_lib.post_request(*args, **kwargs)
    except Exception as ex:
        raise ex
    finally:
        BuiltIn().set_log_level(current_level)

    return result

And the same, in robotframework syntax:

Post Request With No Logging
    [Documentation]     Runs RequestsLibrary's Post Request, with its logging surpressed
    [Arguments]     @{args}     &{kwargs}
    ${current level}=       Set Log Level   ERROR
    ${result}=      Post Request    @{args}     &{kwargs}

    [Return]    ${result}
    [Teardown]      Set Log Level   ${current level}

The python's version is bound to be milliseconds faster - no need to parse & match the text in the RF syntax, which on large usage may add up.

Upvotes: 1

A. Kootstra
A. Kootstra

Reputation: 6981

Perhaps not the answer you're looking for, but after having looked at the source of the RequestsLibrary I think this is indeed undesirable and should be corrected. It makes sense to have the file contents when running in a debug or trace setting, but not during regular operation.

As I consider this a bug, I'd recommend registering an issue with the GitHub project page or correcting it yourself and providing a pull request. In my opinion the code should be refactored to send the file name under the info setting and the file contents under the trace/debug setting:

logger.info('Post Request using : alias=%s, uri=%s, data=%s, headers=%s, allow_redirects=%s' % ...
logger.trace('Post Request files : files=%s' % ...

In the mean time you have two options. As you correctly said, temporarily reduce the log level settings in Robot Code. If you can't change the script, then using a Robot Framework Listener can help with that. Granted, it would be more work then making the change in the ReqestsLibrary yourself.

An temporary alternative could be to use the RequestLibrary Post, which is deprecated but still present.

Upvotes: 0

Related Questions