Reputation: 835
Stats.print_stats() print the output to std_output by default, or you can give a stream to the Stats. How can I apply logging into this stream?
I have a logging handler to a file :
log = logging.getLogger('log1') debughandler = logging.FileHandler("debug." + LogFilename, 'w', 'utf-8') debughandler.setFormatter(formatter) debughandler.setLevel(logging.DEBUG) log.addHandler(debughandler)
now I just want to redirect the output of Stats into this log handler.
Thanks.
Upvotes: 1
Views: 665
Reputation: 835
Use a io.StringIO() as a stream to hold the result:
s = io.StringIO()
pstat = pstats.Stats('restatslex', stream=s)
pstat.sort_stats('time').print_stats(20)
log.info(s.getvalue())
then the stats information is in the log.
Upvotes: 3