Skip to content

Move loggers to module level definitions.

Cal Pratt requested to merge cpratt34/module-logger-cleanup into master

Move loggers to module level definitions.

Using logging.getLogger(__name__) will always return a logger associated with the name of the module, and therefore should not be associated with any instance of a class. This change moves the getLogger calls to the module level which simplifies constructor setups, provides an easier way to access the loggers in top-level functions (non-class based), and provides a consistent naming convention for accessing the logger instance.

In cases of inheritance, this pattern avoid bugs. With the old style, if a child class declares the same variable name as the parent class for the logger instance (e.g. self.logger), then depending on the ordering of super().__init__, either the child or parent class will report the wrong module. This is subtle issue can make it very difficult to debug root causes of issues when examining log files.

Edited by Cal Pratt

Merge request reports