Add memory info to python logging: several options?
Our program is using a lot of memory. Too much of memory, maybe. So I want to write memory of current process in logging information.
Getting the memory size of current process is the easy part (in Linux):
resource.getrusage(resource.RUSAGE_SELF).ru_maxrssI can think of several ways to add this into logging system: Adding a handler (derive the handler from either StreamHandler or FileHandler), or change the Formatter. Finally I decided to add a filter:
class MemFilter(logging.Filter):
def filter(self, record):
record.mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
return True
logging.basicConfig(format='[%(levelname)s][MEM:%(mem)s] [%(asctime)s]:%(message)s')
logging.root.addFilter(MemFilter())
Case closed.
Labels: Programming
0 Comments:
<< Home