title: Python - Question on logging in __init__.py
body: <p>I have the following python package structure.</p>
  
  <pre><code>python_logging
      python_logging
          __init__.py
          first_class.py
          second_class.py
      run.py
  </code></pre>
  
  <hr>
  
  <p>Here is the code in <code>__init__.py</code></p>
  
  <blockquote>
    <p><strong>init</strong>.py</p>
  </blockquote>
  
  <pre><code>import logging
  import logging.config
  
  
  # Create the Logger
  loggers = logging.getLogger(__name__)
  loggers.setLevel(logging.DEBUG)
  
  # Create the Handler for logging data to a file
  logger_handler = logging.FileHandler(filename='C:\Python\Log\stest.txt')
  logger_handler.setLevel(logging.DEBUG)
  
  # Create a Formatter for formatting the log messages
  logger_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
  
  # Add the Formatter to the Handler
  logger_handler.setFormatter(logger_formatter)
  
  # Add the Handler to the Logger
  loggers.addHandler(logger_handler)
  loggers.info('Completed configuring logger()!') 
  </code></pre>
  
  <hr>
  
  <p>Here is code for first_class.py</p>
  
  <pre><code>import logging
  
  class FirstClass(object):
      def __init__(self):
          self.current_number = 0
          self.logger = logging.getLogger(__name__)
  
  
      def increment_number(self):
          self.current_number += 1
          self.logger.warning('Incrementing number!')
          self.logger.info('Still incrementing number!!')
  </code></pre>
  
  <hr>
  
  <p>Here is code for second_class.py</p>
  
  <pre><code>class SecondClass(object):
      def __init__(self):
          self.enabled = False
          self.logger = logging.getLogger(__name__)
  
      def enable_system(self):
          self.enabled = True
          self.logger.warning('Enabling system!')
          self.logger.info('Still enabling system!!')
  </code></pre>
  
  <hr>
  
  <p>Here is code for run.py</p>
  
  <pre><code>from LogModule.first_class import FirstClass
  from LogModule.second_class import SecondClass
  
  number = FirstClass()
  number.increment_number()
  system = SecondClass()
  system.enable_system()
  </code></pre>
  
  <p>This is the output in the log file</p>
  
  <pre><code>LogModule - INFO - Completed configuring logger()!
  LogModule.first_class - WARNING - Incrementing number!
  LogModule.first_class - INFO - Still incrementing number!!
  LogModule.second_class - WARNING - Enabling system!
  LogModule.second_class - INFO - Still enabling system!!
  </code></pre>
  
  <p><strong>Question :</strong> How did number.increment_number() and system.enable_system()  write to a log file when the file handler was initialized in <strong>init</strong>.py ? also both the classes have different getloggers . Can anyone explain , it will be helpful.</p>