Over time, I wrote a bunch of scripts, and I save DRY code in the process of refactoring scripts. I am currently using something in these lines in various scenarios:
if __name__ == '__main__': logger = logging.getLogger('dbinit') hdlr = logging.FileHandler('/var/logs/tmp/foo.log') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.WARNING)
Instead of repeating this in every script (ie, “module”), I would like this initialization of the login to be done somewhere and available for different scripts (Hmm, perhaps wrapping in a singleton class?).
If I cannot do this (i.e. put the log initialization code in one main module), I assume that using the same log file name in the logging.FileHandler () call, different scripts will be written to the same file .
Is this assumption correct?
Last but not least, what is the best practice (i.e. Pythonic) to solve this problem?
source share