I wnat to log all file write operations across scripts, and I want the info of which method/class made the write operation. For example, I have this code
import logging
logger = logging.getLogger('root')
#FORMAT = "[%(filename)s:%(funcName)20s() ] %(message)s"
#logging.basicConfig(format=FORMAT)
logger.setLevel(logging.DEBUG)
class FileWrite:
def __init__(self,filename):
self.filename = filename
self.f = open(filename,'w')
def __enter__(self):
return self.f
def __exit__(self):
self.f.close()
logger.debug("Write to: {}".format(self.filename))
I want to be able to also log which method made the write operation, when I run code like this
class A:
def a():
with FileWrite("file.txt") as f:
f.write("something")
It should be logged that "file.txt" was written by method A.a