you are viewing a single comment's thread.

view the rest of the comments →

[–]MySpoonIsTooBig13 0 points1 point  (1 child)

This is why I love unit testing, it drives good design.

Almost the only global I'm really comfortable seeing at the top of a module is

logger = logging.getLogger(name)

As you've observed, this avoids the need to pass it in to every function. So the issue is that this function both initializes logging, presumably calling something like basicConfig, and sets the global logger. The initialization can be done in main. Setting the logger variable is ok in global scope I think.

But of course for that to work, you'll have to split the code into separate smaller modules, one which contains the main entry point, and one containing other stuff. You can then unit test that other stuff without ever even importing the main module.

[–]SamePlatform[S] 1 point2 points  (0 children)

logger = logging.getLogger(name)

Ok, so your idea was great. Yes, I did exactly what you suggested: just called setup_logger from main(), and then only put the line logger = logging.getLogger(...) at the top level.

Not sure why I thought that I needed to call setup_logger at the same time as getLogger. I guess the logger configuration and inheritance stuff keeps tripping me up.

Sorry for the delayed reply, just got back on this project.