title: Object throwing multiple instances even though it's configured for a single instance
body: <p>I am coding in Python and I am facing a situation where my object is throwing out multiple instances even though it has been configured for a single instance. So I have multiple modules that is running a script and there is one module called mylogger.py which instantiates the logger object that stores and displays the logs of the script in a file and the console respectively. I am calling the module mylogger.py from multiple modules in my script. But I want to make sure that for those multiple calls from different modules I use the same initial object so that I do not get the same lines being inserted multiple times in my log file. But I am not sure even after proper configuration I am seeing multiple objects being created each time the function setLogger() is called from different modules.</p>
  <pre><code># mylogger.py
  
  import logging
  
  logger = None
  
  
  def setLogger(filename=&quot;logfile.txt&quot;):
      global logger
      print(&quot;*********VALUE OF LOGGER IN FUNCTION******** &quot;, logger)
      if logger is None:
          logger = logging.getLogger(__name__)     # creating logger object only once
      if not getattr(logger, 'handler_set', None):
          logger.setLevel(logging.INFO)
          stream_handler = logging.StreamHandler()
          file_handler = logging.FileHandler(filename)
          formatter = logging.Formatter('%(message)s')
          file_handler.setFormatter(formatter)
          logger.addHandler(file_handler)
          logger.addHandler(stream_handler)
          logger.setLevel(logging.INFO)
          logger.propagate = False
          logger.handler_set = True
      return logger
  
  </code></pre>
  <p>I am calling this file from multiple modules. I have included the parts which calls the mylogger.py file.</p>
  <pre><code>#firstModule.py
  import mylogger
  import os 
  import sys
  
  logger = mylogger.setLogger()
  logger.info(&quot;Logger object in firstModule is &quot;, logger)
  # Some code 
  # and print statements
  # goes here
  # Trigger next module by calling it using os.system(...)
  os.system('python' + path_of_script/secondModule.py)
  </code></pre>
  <p>Now the secondModule.py calls the mylogger.py</p>
  <pre><code>#secondModule.py
  import mylogger
  import os 
  import sys
  
  logger = mylogger.setLogger()
  logger.info(&quot;Logger object in secondModule is &quot;, logger)
  
  # Some code
  #and print statements
  #go here
  #And then it calls some other module
  </code></pre>
  <p>So basically the logger object is different for firstModule.py and secondModule.py and for the next subsequent modules which prints out multiple lines of the same statement in the logfile.txt. However, the lines that get printed on the console through streamHandler() is fine as it only prints out single line for a single logger.info(). This seems very weird as both streamHandler() and fileHandler() were configured using the same function.</p>