import os, threading, logging, logging.handlers lock = threading.RLock() def acquire_lock(): lock.acquire() def release_lock(): lock.release() class LoggerThread(threading.Thread): def __init__(self, numLoops, n): threading.Thread.__init__(self) self.name = 'thread-%02d' % n self.numLoops = numLoops def run(self): for x in range(0, self.numLoops): acquire_lock() try: os.system('cd . > blah.txt') os.system('rm blah.txt') finally: release_lock() logging.debug('This is log message ' + str(x) + ' from ' + self.name + ' and I think this should be a little longer, so I\'ll add some more data here because perhaps the size of the individual log message is a factor. Who knows for sure until this simple test fails.') if __name__=="__main__": logSize = 2048 numberOfLogs = 10 files = logging.handlers.RotatingFileHandler('logthred.log', 'a', logSize, numberOfLogs) console = logging.StreamHandler() # set a format fileFormatter = logging.Formatter('%(asctime)s %(levelname)-8s %(thread)-4s %(message)s') consoleFormatter = logging.Formatter('%(asctime)s %(levelname)-8s %(thread)-4s %(message)s') # tell the handler to use this format files.setFormatter(fileFormatter) console.setFormatter(consoleFormatter) # add the handlers to the root logger logging.getLogger('').addHandler(files) logging.getLogger('').addHandler(console) logging.getLogger('').setLevel(logging.DEBUG) numThreads = 10 numLoops = 100 # Create and execute threads threads = [] for x in range(0, numThreads): t = LoggerThread(numLoops, x) threads.append(t) t.start() for t in threads: t.join()