Here is a brute force, simplistic implementation.
Note that any logging.Logger methods you want to call on the epiLogger need to be implemented: I've only implemented info().
import logging
# class definition for the new logger that does the pro and epilogues
class epiLogger():
def __init__(self, name):
self.logger = logging.getLogger(name)
self.logger.info('** My Prologue **')
def info(self, the_info):
self.logger.info(the_info)
def __del__(self):
self.logger.info('** My Epilogue **')
# preamble to set up the underlying logger that it needs
# ... could be inside the class constructor, depending on whether the class
# is supposed to be "encapsulating and providing" or "using" this external one
the_logger = logging.getLogger("foo")
the_logger.setLevel(logging.INFO)
the_logger.addHandler(logging.StreamHandler())
# using the good stuff...
a = epiLogger("foo")
a.info("foo!")
a.info("bar!")
a.info("party!")
produces:
~ mgregory$ python foo.py
** My Prologue **
foo!
bar!
party!
** My Epilogue **
~ mgregory$
(For simplicity I've left out the time/date formatting etc, since it's not really relevant to the question)