3

I just can't get a StreamHandler working with StringIO to accept logging from a Process. The strange thing is that a stream to stdout works just fine.

This is my code:


from time import sleep
import logging
import multiprocessing
from io import StringIO


logBuffer = StringIO()
#logging.basicConfig(level=logging.DEBUG,stream=logBuffer)
logging.basicConfig(level=logging.DEBUG)
logging.debug('This will get logged')


def main():
    for i in range(10):
        logging.debug('i=%i' % i)
        print(i)
        sleep(0.5)

# start process
proc = multiprocessing.Process(target=main )
proc.start()
proc.join()

print(logBuffer.getvalue())
sleep(3)

using logging.basicConfig(level=logging.DEBUG) prints everything to stdout:

DEBUG:root:This will get logged
DEBUG:root:i=0
0
DEBUG:root:i=1
1
DEBUG:root:i=2
2
DEBUG:root:i=3
3
DEBUG:root:i=4
4
DEBUG:root:i=5
5
DEBUG:root:i=6
6
DEBUG:root:i=7
7
DEBUG:root:i=8
8
DEBUG:root:i=9
9

while trying to capture with StringIO with logging.basicConfig(level=logging.DEBUG,stream=logBuffer) captures only current main output:

0
1
2
3
4
5
6
7
8
9
DEBUG:root:This will get logged

Can anybody help? Other solutions for passing real-time output from a Process to the parent process are also welcome.

C.Nivs
  • 12,353
  • 2
  • 19
  • 44
Jev
  • 1,163
  • 10
  • 13

1 Answers1

0

I've found an answer in the logging-cookbook, using a QueueHandler, so I'll answer my own question (RTFM) ;-).

However an easier solution seems to be switching to Threading and passing a logger instance to the trhead function.

Jev
  • 1,163
  • 10
  • 13