In my python script, I am using the logging module with INFO going to a file and the screen:
fh_info = logging.FileHandler(info)
fh_info.setLevel(logging.INFO)
fh_info.setFormatter(formatter)
std_out_info = logging.StreamHandler()
std_out_info.setLevel(logging.INFO)
logger.addHandler(fh_info)
logger.addHandler(std_out_info)
But the issue I am having is that the messages got to the screen after the function is completed. For instance, in:
def getToken(self):
msg = ("Initializing token....")
logger.info(msg)
sys.stdout.flush()
try:
jsonreq = ( {"auth": {"KEY:apiKeyCredentials": {
"username": self.username,
"apiKey": self.apikey}}})
auth_headers = {'content-type': 'application/json'}
r = requests.post(self.url, data=json.dumps(jsonreq), headers=auth_headers)
self.jsonresp = json.loads(r.text)
self.token = str(self.jsonresp['access']['token']['id'])
msg = ("done!")
logger.info(msg)
except:
msg = ("Bad name or apikey!")
logger.error(msg)
sys.exit()
The message "Initializing token...." will go to the screen after the operations in the function are completed. The makes for a long pause while authentication is going on with no output...then after authentication completes I see "Initializing token....done".
How can I make the logger.info(msg) stay in sync with the flow of the script and output to the screen in it's timely manner?