I am trying to store logs from multiple threads into a single file. But in my log file, logs from different threads are getting merged. I don't want separate log files for different threads. But when one thread is writing logs to the file, the other thread should wait till the previous thread finishes writing all the logs. I have tried different ways to do so, but finally didn't find appropriate solution.
from threading import Thread, current_thread
import threading
import time
import logging
logging.basicConfig(filename='LogsThreadPrac.log', level=logging.INFO)
logger = logging.getLogger(__name__)
def worker(val):
print('value from the thread: {}'.format(val))
logger.info('+++++++++++++++++++++++++++++++++++++++')
print(current_thread().name)
logger.info('value from the thread: {}'.format(val))
time.sleep(3)
logger.info('+++++++++++++++++++++++++++++++++++++++')
t1 = Thread(target=worker, args=[1,], name='th'+str(1))
t1.start()
t2 = Thread(target=worker, args=[2,], name='th'+str(2))
t2.start()