I have a Driver.py
scripts where it calls multiple threads
based on the given inputs. Threads are basically runs a module of a selected object. So Driver.py
may call thread_1.run()
, thread_2.run()
, thread_3.run()
, and continue its process.
Driver.py
logs its output into a main.log folder, where I want threads to log their output into unique filenames for each. Driver.py
and Threads
also uses common modules that are defined on differnt files, where they also logs information.
I call setup_load("main.log")
first on Driver
, afterwards in each Thread
, called setup_load(f"thread_{jobid}.log")
as well. I realize that when Thread
is called, now Driver.py writes into thread's log file. I may use a differnt logger inside Thread but when the Thread calls another modules, since those common modules are using import logging
they write into the root logger's defined filename.
=> Is it possible to log messages from different threads to different files? I found multiple answers on SO (for example), but non of them covers when another module is called on a different file, how would they can find out that which logger
they can use.
=> So the problem I am facing is since every thread is using the same underlying logger, when I change the file path of the logging.basicConfig
in one thread, it affects the class across all threads and the driver, since they're all using it.
=> How would be functions from different modules called from the thread or driver would understand which logger should it choose?
Comment section on How to change filehandle with Python logging on the fly with different classes and imports has a discussion and recommended solution.
@Martijn Pieters:
next option: create per-thread handlers, give each of them a filter that filters on the logrecord
thread
attribute. Attach a filter to any other handlers that returns False for logrecords withthread
set