python - Is there any difference in putting the logging statement in a module level vs in a class level? -
module1.py
#!/usr/bin/env python import logging logger = logging.getlogger(__main__) class myclass() def __init__(self): logger.info('test')
versus
module2.py
#!/usr/bin/env python import logging class myclass() def __init__(self): self.logger = logging.getlogger(__main__) self.logger.info('test')
seems me module2.py give unpredictable outcome when imported other modules. i'm not sure though.
logging.getlogger()
returns singleton (per given name); there's no difference in storing module global or instance attribute. code referencing same object in both cases.
quoting documentation:
multiple calls
getlogger()
same name return reference same logger object.
and logging.getlogger()
function itself:
all calls function given name return same logger instance. means logger instances never need passed between different parts of application.
Comments
Post a Comment