I’m subclassing FileHandler to write a handler that produces correct xml files (meaning, not while the program is running, only after finishing).
what I do is:
- create the file,
- write a prologue (a
<Diag>element opening), - each log record causes a
<line />element in the xml - stop logging means write the
</Diag>element closer.
it would be nice if the Logger.removeHandler would call some special callback in the handler being removed, but I can’t find any. maybe it’s not there…
Handlers are not notified when they are added or removed from loggers. Loggers use handlers, but handlers know nothing about loggers.
However, a handler has a
close()method that is called at program exit bylogging.shutdown()(registered withatexit), so you can add your logic there. Just remember to call the base implementation’sclose()to ensure proper housekeeping: