title: Python: Where to put logging.getLogger
body: <p>I tried to put getLogger in the module level. However, it has some disadvantages. Here is my example:</p>
  
  <pre><code>from logging.handlers import TimeRotatingFileHandler
  
  log_monitor = logging.getLogger('monitorlog')
  log_monitor.setLevel(logging.DEBUG)
  log_monitor.propagate = False
  handler = TimedRotatingFileHandler('somedirectory/monitor.log',
                                      when='h',
                                      interval=1,
                                      backupCount=30)
  monitor_format = logging.Formatter('%(asctime)s: %(message)s')
  handler.setFormatter(monitor_format)
  log_monitor.addHandler(handler)
  </code></pre>
  
  <p>The problem is, when some other module import this one, the above code will be executed. It is possible, at that time, the <code>somedirectory</code> does not even exist and the build will fail. </p>
  
  <p>Actually, this logger will be used in a class, so I am thinking of putting <code>getLogger</code> into the class. However, I feel if people create multiple object of that class, that piece of code will be called multiple times. I guess this part of code is supposed only to be called once. </p>
  
  <p>I am pretty new to python. Where do people usually put their <code>getLogger</code> and in this case, where should I put this piece of code?</p>