changeset 1:7edf54ec37f2

Improve logging.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 18 Feb 2020 13:18:25 +1030
parents a5a196b3ba63
children 393a879db9eb
files pw2log.py
diffstat 1 files changed, 17 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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)