I'm trying to add a RotatingFileHandler to my Watchdog, allowing me to keep logfile growth in check. For illustrative purposes, I'll use the Watchdog Quickstart Example.
I found a thread explaining how to implement Pythons' logging RotatingFileHandler, but I get stuck when I try to combine both scripts:
if __name__ == "__main__":
logging.basicConfig(filename='test.log', # added filename for convencience
level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# edited Quickstart example with RotatingFileHandler here
logger = logging.getLogger('test')
handler = RotatingFileHandler("test.log", maxBytes=2000, backupCount=2)
logger.addHandler(handler)
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
The code doesn't generate any errors but keeps logging as usual. I suspect the RotatingFileHandler is only passed to the regular logger and not to the LoggingEventHandler, but I have no idea how to pass it to the correct handler.
Any tips are greatly appreciated,
Regards
Check watchdog's source code about
LoggingEventHandler
: http://pythonhosted.org/watchdog/_modules/watchdog/events.html#LoggingEventHandlerAs you can see,
LoggingEventHandler
just uselogging
to log. What you need to do is implementing your customLoggingEventHandler
.For example:
And then use your custom
LoggingEventHandler
.