1

A polling method is implemented and it works every second to check the request status. Is it possible to add a log for each iteration of the polling?

result = poll(
    lambda: getSomething(), 
    timeout=100, 
    step=1,
    check_success=IsPollingSuccessfull        
)

I need something like,

Waiting for the response + time
Waiting for the response + time
Waiting for the response + time
Waiting for the response + time

EDIT: I want to print log to the console.

Fiver
  • 9,909
  • 9
  • 43
  • 63
Mr.B
  • 61
  • 1
  • 5

1 Answers1

0

Have you considered python's logging? Here is the documentation

you can create a logger instance that saves to all messages to file. Then you can use it everywhere in your code and log anything you'd like with different logging levels.

Here is how I create and use the logger:

# Method to create an instance of a logger

import logging

def set_logger(context, file_name, verbose=False):
    logger = logging.getLogger(context)
    logger.setLevel(logging.DEBUG if verbose else logging.INFO)
    formatter = logging.Formatter(
        '[%(asctime)s][%(levelname)s]:' + context + ':[%(filename).30s:%(funcName).30s:%(lineno)3d]:%(message)s',
        datefmt='%Y-%m-%d %H:%M:%S\x1b[0m')
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG if verbose else logging.INFO)
    console_handler.setFormatter(formatter)
    logger.handlers = []
    logger.addHandler(console_handler)
    file_handler = logging.FileHandler($path_to_save_logger + file_name)
    file_handler.setLevel(logging.DEBUG if verbose else logging.INFO)
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)
    return logger

then create the instance and use it.


from termcolor import colored

my_logger = set_logger(colored('GENERAL', 'yellow'), "/tmp/my_logger.txt", verbose=True)
my_logger.info("this is an info message")
my_logger.debug("this is a debug message")

.....

EDIT: assuming you're using polling2.poll() You can add a logger into you poll() call - documentation

import logging

poll(lambda: getSomething(),
 timeout=100,
 step=1,
 check_success=IsPollingSuccessful,
 log=logging.DEBUG)
pugi
  • 323
  • 1
  • 12
  • thanks, I don't need a file for that. It is OK to print the log in the console. But I could not figure out where to place the logging line in the polling code – Mr.B Jan 19 '22 at 15:41
  • Assuming you're using polling2.poll(), you can add the logging line in your poll(lambda: getSomething(), timeout=100, step=1, check_success=IsPollingSuccessful, log=logging.DEBUG). See here - https://polling2.readthedocs.io/en/latest/examples.html#logging-the-return-values-from-the-target-function – pugi Jan 19 '22 at 15:50
  • Unfortunately, it is polling only – Mr.B Jan 19 '22 at 15:58
  • did you check the answer for this question? https://stackoverflow.com/questions/67663252/how-to-add-polling-interval-for-a-get-request-in-python – pugi Jan 19 '22 at 16:01