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

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -