# HG changeset patch # User Daniel O'Connor # Date 1581994105 -37800 # Node ID 7edf54ec37f20d3a370810c12e36e12a5a00c3e2 # Parent a5a196b3ba633aeda5f7e101eeadf15b8b23294e Improve logging. diff -r a5a196b3ba63 -r 7edf54ec37f2 pw2log.py --- a/pw2log.py Wed Nov 20 13:12:45 2019 +1030 +++ b/pw2log.py Tue Feb 18 13:18:25 2020 +1030 @@ -66,19 +66,32 @@ ch = logging.StreamHandler() ch.setFormatter(fmt) logger.addHandler(ch) + keepfh = None else: fh = RotatingFileHandler(logfile, maxBytes = 2000, backupCount = 10) fh.setFormatter(fmt) logger.addHandler(fh) + keepfhs = [fh.stream.fileno()] # XXX: gross if pidfile == None: ctx = NullContextManager() else: - ctx = daemon.DaemonContext(pidfile = daemon.pidfile.PIDLockFile(pidfile)) + try: + #fh = open('/tmp/pw2errs.log', 'a') + fh = None + ctx = daemon.DaemonContext(pidfile = daemon.pidfile.PIDLockFile(pidfile), stdout = fh, stderr = fh, files_preserve = keepfhs) + except Exception as e: + logger.critical('Unable to get daemon context') - with ctx: - logger.critical('Starting') - collectdata(cp.get('pw', 'ip'), cp.get('db', 'dsn'), cp.getint('db', 'logtime')) + try: + with ctx: + logger.critical('Starting') + try: + collectdata(cp.get('pw', 'ip'), cp.get('db', 'dsn'), cp.getint('db', 'logtime')) + except Exception as e: + logger.critical('Unable to collect data: ' + str(e)) + except Exception as e: + logger.critical('Unable to enter daemon context: ' + str(e)) def collectdata(pwip, dsn, logtime): dbh = psycopg2.connect(dsn)