I seem to find myself putting the same chunk of code at the top of every script that I write in Python;::
import logging MESSAGE_FORMAT = "%(asctime)s %(levelname)s:: %(message)s" DATE_FORMAT="%Y.%m.%d %T" logger = logging.getLogger(self.__name__) logger.setLevel(logging.INFO) formatter = logging.Formatter(MESSAGE_FORMAT, DATE_FORMAT) ch = logging.StreamHandler() ch.setFormatter(formatter) logger.addHandler(ch)
Because the default simple logging provided by the standard library
logging package doesn't do everything I'd like. I prefer that messages contain the time that they were generated. I prefer that the default logging level is set to
INFO. I don't really care to see the log name in each log message.
So I end up with this chunk of code near the top of every script that I write. I've also accumulated a module with very similar set up code and a couple of utility functions in every application that I've written.