5

Following This question and answer, I'm looking for a solution to the following problem:

I have a Python program which supposed to run both on Windows and Linux.

My python process spawns a child process, and both of them should have a file logger. I want each of them to log into their own log file.

process creation:

import multiprocessing
multiprocessing.Process(target=my_target, args=(my_args,))

log creation:

import logging
logger = logging.getLogger()
fh = logging.FileHandler(log_file, mode="a+")
logger.addHandler(fh)

In Windows it works great, but in Linux I got the child output written into both the child and parent log.

What will be the best way to deal with such a scenario?

macro_controller
  • 1,469
  • 1
  • 14
  • 32

1 Answers1

2

Added the following code to solve the issue, at the beginning of execution of the chilld:

log = logging.getLogger()
# Remove any existing handlers
for handler in log.handlers:
    log.removeHandler(handler)
macro_controller
  • 1,469
  • 1
  • 14
  • 32