The --logfile
option mentioned by the others is probably the easiest route. Note that you probably want to log your messages rather than printing them. I think locust sets up a special stdout logger such that print
messages go to the console, but not to the log file.
An alternative to --logfile
is to utilize python's logging system to add your own file appender. This is a good option if you need a rolling file appender, or have a log format that you prefer over the format that locust configures.
Here is an example of how we setup and use logging in our locust test. Notice how each instance of the taskset logs to its own logger. That makes it easy to filter the log file to a single locust. Also note that we log all HTTP errors as warnings.
from locust import HttpLocust, TaskSet, task
import itertools
import logging
import socket
from logging.handlers import RotatingFileHandler
def append_file_logger():
root_logger = logging.getLogger()
log_format = "%(asctime)s.%(msecs)03d000 [%(levelname)s] {0}/%(name)s : %(message)s".format(socket.gethostname())
formatter = logging.Formatter(log_format, '%Y-%m-%d %H:%M:%S')
file_handler = RotatingFileHandler('./locust.log', maxBytes=5 * 1024 * 1024, backupCount=3)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.INFO)
root_logger.addHandler(file_handler)
append_file_logger()
counter = itertools.count()
class FooTaskSet(TaskSet):
def on_start(self):
self.logger = logging.getLogger('locust-%03d' % counter.next())
self.logger.info('Hatching locust')
@task
def send(self):
response = self.client.post(...)
if not response.ok:
self.logger.warn('Error sending post') # TODO add status code, url, and reponse to the log
Final suggestion : If you have multiple tasks, configure them to log all http errors with the same format. Makes it easy to extract data from your log.