title: Logging across sub-modules – how to access parent logger name from sub-modules?
body: <p>I am trying to add logging to my python application that has several modules and submodules. Several sites say to create child loggers in modules. The advantage I see is that the child logger inheriting the parent logging config, it will provide consistency for the logging output (handlers, formatters, ...).</p>
  
  <p>So far I am defining the <code>__main__</code> class logger name in each class and concatenate it with the current class’ name (<code>parentName.childName</code>) to get the module’s class loggers. It does not feel right, nor scalable. How could I improve this so I don’t have to hard code the <code>__main__</code> class logger name in each class? Here is what my code looks like:</p>
  
  <p>Py file that I run:</p>
  
  <pre><code>###app.py
  import logging
  from SubModule1 import *
  
  class myApplication():
      loggerName='myAPI'
      def __init__(self, config_item_1=None,config_item_2=None,...config_item_N=None):
          self.logger=self.logConfig()
          self.logger.info("Starting my Application")
          self.object1=mySubClass1(arg1, arg2, ... argM)
  
  
      def logConfig(self):
          fileLogFormat='%(asctime)s - %(levelname)s - %(message)s'
          consoleLogFormat='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
  
          # create logger
          #logger = logging.getLogger(__name__)
          logger = logging.getLogger(self.loggerName)
          logger.setLevel(logging.DEBUG)
  
          ####CONSOLE HANDLER####
          # create console handler and set level to debug
          ch = logging.StreamHandler()
          ch.setLevel(logging.INFO)
  
          # create formatter
          consoleFormatter = logging.Formatter(consoleLogFormat)
  
          # add formatter to ch
          ch.setFormatter(consoleFormatter)
  
          # add ch to logger
          logger.addHandler(ch)
  
          ####FILE HANDLER####
          # create file handler and set level to debug
          fh = logging.FileHandler('API_Log.txt')
          fh.setLevel(logging.DEBUG)
  
          # create formatter
          fileFormatter = logging.Formatter(fileLogFormat)
  
          # add formatter to ch
          fh.setFormatter(fileFormatter)
  
          # add ch to logger
          logger.addHandler(fh)
  
          logger.info('Logging is started!!')
  
          return logger
  
      def connect(self):
          self.logger.info("Connecting to API")
          self.object1.connect()
  
  
  
  if __name__=="__main__":
      config={"config_item_1": x,
              "config_item_2": y,
              ...
              "config_item_N": z
              }
  
  
      myApp=myApplication(config['config_item_1'],config['config_item_2'],...config['config_item_N'])
      myApp.connect()
  </code></pre>
  
  <p>Module:</p>
  
  <pre><code>###SubModule1.py
  import logging
  import app
  
  class mySubClass1():
      appRootLoggerName='myAPI'
      def __init__(self,item1):
          self.logger=logging.getLogger(self.appRootLoggerName + '.'+mySubClass1.__name__)
          self.logger.debug("mySubClass1 object created - mySubClass1")   
          self.classObject1=item1
  </code></pre>
  
  <p>The line below is the one that is bothering me. What alternative to <code>self.appRootLoggerName + '.'+mySubClass1.__name__)</code> would keep the same logging configuration shared across my application’s modules/classes? </p>
  
  <pre><code>logging.getLogger(self.appRootLoggerName + '.'+mySubClass1.__name__)
  </code></pre>