Python multiple logger for multiple modules
<p>I have two files namley main.py and my_modules.py. In main.py I have defined two loggers like this</p>

<pre><code>    #main.py
    URL_LOGS = "logs/urls.log"
    GEN_LOGS = 'logs/scrape.log'
    #Create two logger files    
    formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
    # first file logger
    url_info_logger = logging.getLogger('URL_Fetcher')
    hdlr_1 = logging.FileHandler(URL_LOGS)
    hdlr_1.setFormatter(formatter)
    url_info_logger.setLevel(logging.DEBUG)
    url_info_logger.addHandler(hdlr_1)

    #second Logger
    general_logger = logging.getLogger("GENERAL")
    hdlr_2 = logging.FileHandler(GEN_LOGS)
    hdlr_2.setFormatter(formatter)
    general_logger.setLevel(logging.DEBUG)
    general_logger.addHandler(hdlr_2)

   module1()
   do_something()
</code></pre>

<p>In my second file (my_modules.py) I have to use both loggers, Following is the sample code for my_modules.py</p>

<pre><code> #my_modules.py   
 import logging

    def module1():
        general_logger.info("Logger Module1")
        url_info_logger.info("New URL found")

    def do_something():
        general_logger.info("Logger Module2")
        url_info_logger.info("Url parsed")    
</code></pre>

<p>How do I implement loggers to be accessed in my_modules.py</p>

<p>Quote from <a href="https://docs.python.org/2/library/logging.html" rel="nofollow">logging documentation</a>: <em>Multiple calls to getLogger() with the same name will always return a reference to the same Logger object.</em></p>

<p>So what you want to do in your my_modules.py is just to call the <code>getLogger()</code> again with the same name.</p>

<pre><code>#my_modules.py   
import logging

url_info_logger = logging.getLogger('URL_Fetcher')
general_logger = logging.getLogger("GENERAL")

def module1():
    general_logger.info("Logger Module1")
    url_info_logger.info("New URL found")

def do_something():
    general_logger.info("Logger Module2")
    url_info_logger.info("Url parsed")    
</code></pre>

<p>It should return the same logging object, since it is already defined before you call it the second time.</p>

python logging