Python multiple logger for multiple modules
Asked Answered
H

2

7

I have two files named main.py and my_modules.py. In main.py I have defined two loggers like this

    #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()

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

 #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")    

How do I implement loggers to be accessed in my_modules.py.

Haff answered 27/9, 2016 at 7:42 Comment(0)
D
15

Quote from logging documentation: Multiple calls to getLogger() with the same name will always return a reference to the same Logger object.

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

#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")    

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

Dourine answered 27/9, 2016 at 7:55 Comment(1)
Just a little modification. ALso call getLogger for second logger like general_logger = logging.getLogger("GENERAL")Haff
B
1

if you transform the second module in a class, you can simply:

  • declare the logger in the first modulue
  • create the new class passing the 2 logger as parameters
  • use the logger in the new class

Example:

import logging

class MyClassName:

    def __init__(self, general_logger, url_info_logger):
    self.general_logger = general_logger
    self.url_info_logger = url_info_logger

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

    def do_something(self):
    self.general_logger.info("Logger Module2")
    self.url_info_logger.info("Url parsed")

In the main

#main.py
...
myobj = MyClassName(general_logger,url_info_logger)
myobj.do_something()
myobj.module1()
Bate answered 27/9, 2016 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.