1

I want to create my custom logger class

import logging
class mylooger():
   def __init__(module_name):
       logger=logging.getlogger(module_name)

And i want use logger in my main file and two other files

main.py
test1.py
test2.py

i want to decide my logfile path in main.py and keep main.py, test1.py and test2.py file log in same file.

Now suppose later i want to import test1.py and test2.py in some other file e.g main1.py. So i want to decide my logfile path from main1.py and keep main1.py, test1.py and test2.py log in same file.

1
  • 1
    Unclear what you are asking. logger is just a local variable to __init__, and getLogger already returns a reference to a unique Logger object based on its argument. Commented Feb 12, 2019 at 16:47

1 Answer 1

1

That's not how stdlib logging is designed. If you want to use the same logger in multiple modules, just get the same logger:

# in main.py
logger = logging.getLogger("mylogger")

def main():
    ...
    logging.basicConfig(...)
    logger.info("some event")

And:

# in test1.py
logger = logging.getLogger("mylogger")

def some_lib_function():
    ...
    logger.debug("some other event")

The logging framework itself maintains a global mutable state so that these are resolving to the same loggers and hence the same formatters/handlers.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.