Answer the question
In order to leave comments, you need to log in
Why do logs overwrite each other in multi-threaded usage?
import logging
import logging.handlers
_log = logging.getLogger('DEBUG')
logfile = 'logs/debug.log'
formatter = logging.Formatter('%(asctime)s %(message)s')
handler = logging.handlers.TimedRotatingFileHandler(logfile,
when='midnight',
backupCount=10)
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
_log.setLevel(logging.DEBUG)
_log.addHandler(handler)
Answer the question
In order to leave comments, you need to log in
Strangely, the Python logger flush-it every entry
is possibly a problem with log rotation, it is not designed for multi-user recording
. For multi-threaded logs, it is better to raise some kind of server and send logs to it via sockets. The rotation must be done by someone alone.
The script in 10 threads works constantly and once a day it writes a log for a new day, but a bug gets out on a new day.
Each thread creates a file when it needs to write something to the log, and thus the file is recreated 10 times and the logs are not saved (
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question