Python-daemon: keep logging
python, python-2.7
Solution
What about configuring your logger inside your daemon ? This works for me :
#!/usr/bin/env python
import daemon
import logging
import logging.handlers
from time import sleep
from datetime import datetime
def time_logging_daemon():
logger = logging.getLogger('time_logging_daemon')
logger.addHandler(logging.handlers.SysLogHandler(address='/dev/log'))
logger.setLevel(logging.INFO)
while True:
logger.info(datetime.now())
sleep(1)
with daemon.DaemonContext():
time_logging_daemon()
Problem
I have a script that logs a bit of data to disk: `logging.basicConfig(filename='davis-debug.log',level=logging.DEBUG) logging.basicConfig(filename='davis-error.log',level=logging.ERROR) logging.basicConfig(filename='davis-error.log',level=logging.WARNING) logging.basicConfig(filename='davis-error.log',level=logging.CRITICAL)` When i use python-daemon like this, the logging stops. ``` try: with daemon.DaemonContext(): station = VantageProStation() station.run() except KeyboardInterrupt: logging.critical('Stopping user aborted with CTRL+C') pass ``` I have tried file_preserve, but logging.basicConfig does not return a stream. Also i cannot pass several streams using files_preserve..? I afcourse want my logging to continue, i tried to put the log definion inside my class init that did not help either.