If I have interpreted the python3 documentation correctly, regardless of where I add the handler to the root logger, my log messages from within a Process should be sent to a file. However, this is not the case.
Here is a complete program which demonstrates the problem:
import logging
import logging.handlers
from multiprocessing import Process
from time import sleep
fileHandler = logging.FileHandler( 'mylog.log' )
fileHandler.setLevel( 5 )
logger = logging.getLogger( None )
def process_logger():
print( "process waiting" )
sleep( 5 )
print( 'process start' )
logger = logging.getLogger( None )
for num in range( 1, 10 ):
logger.critical( "critical: %d" % num )
sleep(1)
#
# if this is where the handler is added, the critical messages
# will go to the file
#
logger.addHandler( fileHandler )
processLogger = Process( target=process_logger )
processLogger.start()
#
# if this is where the handler is added, the critical messages
# will not go to the file.
#
#logger.addHandler( fileHandler )
print( "started" )
I am using python 3.4.3 on OS X 10.10.4.
My guess as to why this does not work is that when I start the Process, it makes a copy of the current environment and if the handler has not been added by that time, the environment the Process is running in won't have it.
I have three questions:
Why do my messages not always end up in the file regardless of where I call logger.addHandler?
Should my messages always end up in the file?
If they should not, what is the best solution to the general problem of needed to manipulate handlers after a Process has been started?