2

I just started to use Python logging module and I cannot understand something.

I'm writing a script that works in the following way:

1st part (single process): it get some data to compute (it's not important how). Here I create a logger in the following way:

import logging
logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('Pipeline')
logger.setLevel(logging.INFO)
logger.warning('Pipeline started')

In other words I log to the screen.

2nd part (multiprocessing): I create several processes (the data analysis is really time and CPU consuming) to analyze the data found before.

Now I would like that each process logs to a different file ONLY without logging to the screen.

What I wrote is :

fh = logging.FileHandler('/tmp/'+multiprocessing.current_process().name+'_worker.log')
fmt = logging.Formatter(%(asctime)-6s: %(name)s - %(levelname)s - %(message)s)
fh.setFormatter(fmt)
local_logger = logging.getLogger(multiprocessing.current_process().name+'_worker')
local_logger.addHandler(fh)
local_logger.warning(multiprocessing.current_process().name + ' (worker) Process started')

What I got is that each process logs to a different file but logs ALSO to the screen!

How can I fix this?

Giovanni Di Milia
  • 13,480
  • 13
  • 55
  • 67

2 Answers2

4

Suspect that your local logger is passing its log messages upwards to the top level, where it gets output to stdout. Try turning off propagation on the local logger. I believe you can do it like this:

local_logger.propagate = False
Art Swri
  • 2,799
  • 3
  • 25
  • 36
  • I would probably add that using basicConfig() may not be the most suitable choice for your situation IMHO. As my colleague Jason pointed out to me, it's really meant for very simple logging setup. I'd recommend looking into using a somewhat more 'sophisticated' approach; easy to find several approaches by googling. – Art Swri May 18 '12 at 22:07
3

You could do it Art Swri's way, or just omit the basicConfig() call. That's what adds a console handler to the root logger.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191