title: logging in multiple classes with module name in log
body: <p>I want to use the logging module instead of printing for debug information and documentation.
  The goal is to print on the console with DEBUG level and log to a file with INFO level.</p>
  
  <p>I read through a lot of documentation, the cookbook and other tutorials on  the logging module but couldn't figure out, how I can use it the way I want it. (I'm on python25)</p>
  
  <p>I want to have the names of the modules in which the logs are written in my logfile.</p>
  
  <p>The documentation says I should use <code>logger = logging.getLogger(__name__)</code> but how do I declare the loggers used in classes in other modules / packages, so they use the same handlers like the main logger? To recognize the 'parent' I can use <code>logger = logging.getLogger(parent.child)</code> but where do I know, who has called the class/method?`</p>
  
  <p>The example below shows my problem, if I run this, the output will only have the <code>__main__</code> logs in and ignore the logs in <code>Class</code></p>
  
  <p>This is my <strong>Mainfile:</strong></p>
  
  <pre><code># main.py
  
  import logging
  from module import Class
  
  logger = logging.getLogger(__name__)
  logger.setLevel(logging.DEBUG)
  
  # create file handler which logs info messages
  fh = logging.FileHandler('foo.log', 'w', 'utf-8')
  fh.setLevel(logging.INFO)
  
  # create console handler with a debug log level
  ch = logging.StreamHandler()
  ch.setLevel(logging.DEBUG)
  
  # creating a formatter
  formatter = logging.Formatter('- %(name)s - %(levelname)-8s: %(message)s')
  
  # setting handler format
  fh.setFormatter(formatter)
  ch.setFormatter(formatter)
  
  # add the handlers to the logger
  logger.addHandler(fh)
  logger.addHandler(ch)
  
  if __name__ == '__main__':
      logger.info('Script starts')
      logger.info('calling class Class')
      c = Class()
      logger.info('calling c.do_something()')
      c.do_something()
      logger.info('calling c.try_something()')
      c.try_something()
  </code></pre>
  
  <p><strong>Module:</strong></p>
  
  <pre><code># module.py
  
  imnport logging
  
  class Class:
      def __init__(self):
          self.logger = logging.getLogger(__name__) # What do I have to enter here?
          self.logger.info('creating an instance of Class')
          self.dict = {'a':'A'}
      def do_something(self):
          self.logger.debug('doing something')
          a = 1 + 1
          self.logger.debug('done doing something')
      def try_something(self):
          try:
              logging.debug(self.dict['b'])
          except KeyError, e:
              logging.exception(e)
  </code></pre>
  
  <p>Output in <strong>console:</strong></p>
  
  <pre><code>- __main__ - INFO    : Script starts
  - __main__ - INFO    : calling class Class
  - __main__ - INFO    : calling c.do_something()
  - __main__ - INFO    : calling c.try_something()
  No handlers could be found for logger "module"
  </code></pre>
  
  <p>Besides: Is there a way to get the module names were the logs ocurred in my logfile, without declaring a new logger in each class like above? Also like this way I have to go for <code>self.logger.info()</code> each time I want to log something. I would prefer to use <code>logging.info()</code> or <code>logger.info()</code> in my whole code.</p>
  
  <p>Is a global logger perhaps the right answer for this? But then I won't get the modules where the errors occur in the logs...</p>
  
  <p>And my last question: Is this pythonic? Or is there a better recommendation to do such things right.</p>