log2d icon indicating copy to clipboard operation
log2d copied to clipboard

Testing: `test_log2d.cleanup()` does not work as expected/required

Open MikeDP opened this issue 2 years ago • 2 comments

(Re)Work on find is complete pending testing but this has highlighted an issue with test_log2d.cleanup() which may impact testing in other areas besides find.

In cleanup(), logging.shutdown() does not have the anticipated effect of re-initialising logging. In particular, after shutdown(), logging still contains handlers which end up affecting subsequent Log instances. Thus, the order in which tests are carried out affects the test results.

This is a design feature of logging.shutdown() which is really meant to be used just before application exit: See this from stackoverflow and Python logging docs.

I will look for a solution but (as always) need to learn more about logging first.

Mike

MikeDP avatar Jan 10 '23 08:01 MikeDP

Thanks Mike and belated Happy New Year! No urgency for this as .shutdown was only intended as a utility/helper function for the testing docs and isn't a "public" part of the log2d functionality. Having said that, if you have the time and inclination to get to the bottom of things, you'll have truly mastered the built in logging module I think, and others (including me) will benefit from the sum of your learnings :)

All the best, Pete

PFython avatar Jan 10 '23 09:01 PFython

Happy New Year also.

You flatterer! The cure seems to be actually in Log.__init__. If you remove any existing handlers before adding the required ones back in, all is well.

Modify __init__ to add

        while len(self.logger.handlers) > 0:
            self.logger.removeHandler(self.logger.handlers[0])

just before the line for handler in self.get_handlers():

This actually fixed another couple of odd behaviours I'd seen - multiple copies of the log message on stdout being a common one.

I'll leave this one to you if that's OK.

Mike

MikeDP avatar Jan 10 '23 11:01 MikeDP