2

I have a Python script, which sends requests to our API using Requests module, then recieve the ansver, parse it and says that's everything is ok or not. The result displays with 'print'. Here is simple example:

for i in response:
    if i in response_keys:
        print('[Request_name] '+i+' is ok')
    else:
        print('[Request_name] Extra key ' +i+' in resronse)

Now I need to log these prints ti file. When I use Logging module like this:

#Example without sending a request, just logging configuration and check
import logging
logging.basicConfig(filename='Autotests_log.log', level=logging.INFO, format='%(asctime)s %(message)s' )


for i in response:
    if i in response_keys:
        logging.info('[Request_name] '+i+' is ok')
    else:
        logging.info('[Request_name] '+i+' is not ok')

I see this in the log file:

2016-04-20 14:24:44,823 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:44,873 [Request_name] success is ok
2016-04-20 14:24:44,874 [Request_name] statusCode is ok
2016-04-20 14:24:44,874 [Request_name] data is ok
2016-04-20 14:24:44,876 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:44,924 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:44,974 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:45,017 Starting new HTTP connection (1): 192.168.1.44

What should I do to see just this in log:

2016-04-20 14:24:44,873 [Request_name] success is ok
2016-04-20 14:24:44,874 [Request_name] statusCode is ok
2016-04-20 14:24:44,874 [Request_name] data is ok
2
  • You can set individual logging thresholds for file and console logging. Then set the log-level for your messages. See this link for detailed info: docs.python.org/2/howto/… Commented Apr 20, 2016 at 9:08
  • you could add log level to your initial format to see what you want to exclude Commented Apr 20, 2016 at 9:26

1 Answer 1

1

Try to configure requests logging level

import logging
logging.getLogger("requests").setLevel(logging.WARNING)
Sign up to request clarification or add additional context in comments.

1 Comment

It works! Thank you very much! This code sets logging lever for the request module? For examole, I can use any other module in brackets?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.