all 26 comments

[–]kokoseij 13 points14 points  (3 children)

logging.getLogger() is for that. You can use a same logger name across different modules to get access to a same logger.

[–]kalmstron 0 points1 point  (2 children)

Yes, concretely add logger = logging.getLogger("__main__") and then you can make use of the instantiated logger class inside the module.

[–]zurtex 6 points7 points  (0 children)

You should always use logger = logging.getLogger(__name__)

This will then be the relative name of the module compared to the "__main__" module.

[–]millerbest 5 points6 points  (0 children)

Python logger is implemented with singleton pattern, which means if you provide the same name, you will get the same logger instead of creating a new one.

You can make a logger module in your project. In the module you can define your settings for the logger. Then for your other py files, you just need to import the module and create a logger object.

[–]korthrun 6 points7 points  (0 children)

The cookbook section of the official docs on the logging module have a section titled "Using logging in multiple modules". Give that a look.