I would like to call a function everytime, I do any sort of logging using the logging
module in python.
As can be seen below, instead of calling copyfiles
at each instance of logging, I would like to see if there is way to injest copyfiles to the filehandler in someway or even a wrapper.
from sys import argv
import shutil
import logging
logger = logging.getLogger('')
sh = logging.StreamHandler(sys.stdout)
logfile = 'logs/log-run-{}.log'.format(date.today().strftime('%d-%m-%Y'))
fh = logging.FileHandler(logfile)
formatter = logging.Formatter('[%(asctime)s] %(levelname)s \
[%(filename)s.%(funcName)s:%(lineno)d] \
%(message)s', datefmt='%a, %d %b %Y %H:%M:%S')
logger.setLevel(logging.INFO)
sh.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(sh)
logger.addHandler(fh)
LOGPATH = args[1]
def copyfiles():
"""
Copy files to azure blob storage
"""
fh.flush()
shutil.copy(logfile, LOGPATH)
logging.info('test')
copyfiles()
logging.info('foobar')
copyfiles()
I tried digging into invoking copyfiles each time logging is called, but I ended up no where.
In case you are wondering why I am copy files from logging, this is why.