0

I would like to set up a logging hierarchy for my project using python's logging module and the dot '.' name structure described here: https://docs.python.org/2/library/logging.html#logging.getLogger

For a given module (bar.py), I would like to set up the logger something like:

log = logging.getLogger('x.bar')

Where I can then use log.(debug/info/warning/etc...) throughout the bar.py module. I know that bar.py will never be run directly, but functions within it could possibly be called from several other modules - so I don't know advance what to set 'x' to in the code snippet. I want bar.py to inherit the logging handlers and formatters from it's caller (x).

2

1 Answer 1

0

The standard approach of having a module-wide logger instantiated via logging.getLogger('mymodule') is not tailored for your intended behaviour. What you should do instead is to let the callers pass the logger as an argument to the functions in your module

# mymodule.py
def foo(plogger, a, b):
    logger=plogger.getChild('bar')
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.