0

I have found a way to create log files on LocustIO. It successfully writes all logs in the file.

Is there a way that the terminal display the logs and writes it on the log file as well? Just so I could easily monitor the results without opening the log file every time.

Tried programmatically but still does not print it.

Nothing prints after running locust -f my_locust_file.py --logfile=locustfile.log

@task(1)
def fetch_records(self):
... 
    response = self.client.get(full_result, auth=login_creds, headers=headers)
    entry_log = "Fetch Records | Username: {}\tPassword: {} | Response: {}".format(self.username, self.password, response)
    logging.info(entry_log)
    print(entry_log)

UPDATE
Tried using python loggers as well and running locust -f my_locust_file.py without --logfile. It is displaying in console but not outputting in logfile.

def on_start(self):
    logging.basicConfig(filename=my_logfile, level=logging.INFO)
Dee
  • 401
  • 3
  • 9
  • 22
  • 1
    Add a second handler that logs to stderr. – Klaus D. Dec 03 '18 at 03:46
  • Tried the first two here https://stackoverflow.com/questions/14058453/making-python-loggers-output-all-messages-to-stdout-in-addition-to-log-file. Not really familiar with loggers and handlers. Can you provide me an example? – Dee Dec 03 '18 at 04:24

1 Answers1

0

Adapted a way from here. Best to place in class level so that you wont encounter duplicate logs in the file.

log = logging.getLogger()
log.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

fh = logging.FileHandler('locustfile.log')
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
log.addHandler(fh)

log.info("Fetch Records | Username: {}\tPassword: {} | {}".format(self.username, self.password, response))
Dee
  • 401
  • 3
  • 9
  • 22