For print messages I use:
import contextlib
def thread_proof_print_capture(thread_number):
path = f'path/to/some/dir/thread_{thread_number}.txt'
with open(path, 'w') as f:
with contextlib.redirect_stdout(f):
print('Hello, World')
# the file thread_{thread_number}.txt contains all prints that would've been printed in this thread
How to thread-safely capture stdout output from a function call?
But I also would like to redirect log messages that are printed to stdout. For logging I use:
LOGGING_HANDLER_LIST = [logging.StreamHandler(), logging.FileHandler("test.log")] # stdout and to file
LOGGER_FORMAT = "%(asctime)s [%(levelname)s] %(message)s"
logging.basicConfig(format=LOGGER_FORMAT, handlers=LOGGING_HANDLER_LIST)
log = logging.getLogger("test")
log.setLevel(logging.DEBUG)
I initially thought that the stdout redirect would also redirect logging.StreamHandler()
but that's not happening. Is there a way to also redirect what gets logged to stdout?
The main intend is to make a program that was not intended to run multi-threaded to log each thread to its separate file without having to change it much. The program itself works with a global logger so it would work if you start it as a sub-processes but as they are run as threads within python the global variables would overwrite each other, resulting in not separated log files.