title: Ideas on dynamic python logging
body: <p>I have an app which runs several instances of main app depending on external parameters. The main app imports few libraries which in turn import other libraries. They all have a global </p>
  
  <p><code>LOGGER = logging.getLogger('module_name')</code></p>
  
  <p>The logger is configured as file handler so all the logs get written to an external file (all logs written to same file). Now I want to write log to different files based on a certain name that is passed to the main app. I need some thing like </p>
  
  <p><code>LOGGER = logging.getLogger(dynamic_criteria_name)</code></p>
  
  <p>The result will be multiple log files dynamic_criteria_name.log, dynamic_criteria_name.log, etc created and any time logger is called from any of the module it should write to the correct file based on the criteria it was called under.</p>
  
  <p>But the problem is the LOGGER is global. I can pass the logger or the <code>dynamic_criteria_name</code> to each function to write to log but it sounds wrong some how. May be I'm just paranoid! I have modules which have sometimes only functions in them. I don't want to pass the logger all around I guess.</p>
  
  <p>I thought about AOP for a while but I don't think it is really cross cutting as it is dynamically generated the logger looks to me as cross cutting but within one instance of the main app. I have thought about other ways to hack global states but I think the dynamic nature makes it all not possible at least in my head. </p>
  
  <p>Below is the pseudocode (I haven't tried it running it) but it explains better what I'm talking about I think. As you can see module_1 imports module_2 and module_3, they both have a global LOGGER, and module_3 calls module_4. I'd like to find out if I can write to the separate log file from module_2 or module_3 without passing name explicitly to each imported module function. I can add multiple handler to logger with different file name but how can I refer to the correct logger from another module given they are all global at the moment. </p>
  
  <pre><code># module_1.py
  import logging
  import time
  
  import module_2
  import module_3
  
  LOGGER = logging.getLogger()
  
  
  def start_main_loop(name):
      while True:
          module_2.say_boo()
          module_3.say_foo()
          LOGGER.debug('Sleeping...')
          time.sleep(10)
  
  if __name__ == '__main__':
      logging.basicConfig(level=logging.DEBUG)
      for i in xrange(10):
          start_main_loop(i)
  
  #----------------------------------------------------        
  # module_2.py
  
  import logging
  
  LOGGER = logging.getLogger()
  
  def say_boo():
      msg = 'boo'
      LOGGER.debug(msg)
      LOGGER.debug(say_twice(msg))
  
  def say_twice(msg):
      LOGGER.debug('Called say twice')
      return msg * 2
  
  #----------------------------------------------------    
  # module_3.py
  
  import logging
  
  import module_4
  
  LOGGER = logging.getLogger()
  
  def say_foo():
      msg = 'foo'
      LOGGER.debug(msg)
      LOGGER.debug(say_twice(msg))
      module_4.say_bar()
  
  def say_twice(msg):
      LOGGER.debug('Called say twice')
      return msg * 2
  
  #----------------------------------------------------    
  # module_4.py
  
  import logging
  
  LOGGER = logging.getLogger()
  
  def say_bar():
      msg = 'bar'
      LOGGER.debug(msg)
  </code></pre>
  
  <p>I'm willing to explore any ideas people might have. Hope I have explained myself clearly if not please let me know, I can rephrase the question. Thanks!</p>