Mercurial > ~darius > hgwebdir.cgi > pw2log
comparison pw2log.py @ 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 |
comparison
equal
deleted
inserted
replaced
0:a5a196b3ba63 | 1:7edf54ec37f2 |
---|---|
64 fmt = logging.Formatter('%(asctime)s: %(message)s', datefmt = '%Y/%m/%d %H:%M:%S') | 64 fmt = logging.Formatter('%(asctime)s: %(message)s', datefmt = '%Y/%m/%d %H:%M:%S') |
65 if logfile == None: | 65 if logfile == None: |
66 ch = logging.StreamHandler() | 66 ch = logging.StreamHandler() |
67 ch.setFormatter(fmt) | 67 ch.setFormatter(fmt) |
68 logger.addHandler(ch) | 68 logger.addHandler(ch) |
69 keepfh = None | |
69 else: | 70 else: |
70 fh = RotatingFileHandler(logfile, maxBytes = 2000, backupCount = 10) | 71 fh = RotatingFileHandler(logfile, maxBytes = 2000, backupCount = 10) |
71 fh.setFormatter(fmt) | 72 fh.setFormatter(fmt) |
72 logger.addHandler(fh) | 73 logger.addHandler(fh) |
74 keepfhs = [fh.stream.fileno()] # XXX: gross | |
73 | 75 |
74 if pidfile == None: | 76 if pidfile == None: |
75 ctx = NullContextManager() | 77 ctx = NullContextManager() |
76 else: | 78 else: |
77 ctx = daemon.DaemonContext(pidfile = daemon.pidfile.PIDLockFile(pidfile)) | 79 try: |
80 #fh = open('/tmp/pw2errs.log', 'a') | |
81 fh = None | |
82 ctx = daemon.DaemonContext(pidfile = daemon.pidfile.PIDLockFile(pidfile), stdout = fh, stderr = fh, files_preserve = keepfhs) | |
83 except Exception as e: | |
84 logger.critical('Unable to get daemon context') | |
78 | 85 |
79 with ctx: | 86 try: |
80 logger.critical('Starting') | 87 with ctx: |
81 collectdata(cp.get('pw', 'ip'), cp.get('db', 'dsn'), cp.getint('db', 'logtime')) | 88 logger.critical('Starting') |
89 try: | |
90 collectdata(cp.get('pw', 'ip'), cp.get('db', 'dsn'), cp.getint('db', 'logtime')) | |
91 except Exception as e: | |
92 logger.critical('Unable to collect data: ' + str(e)) | |
93 except Exception as e: | |
94 logger.critical('Unable to enter daemon context: ' + str(e)) | |
82 | 95 |
83 def collectdata(pwip, dsn, logtime): | 96 def collectdata(pwip, dsn, logtime): |
84 dbh = psycopg2.connect(dsn) | 97 dbh = psycopg2.connect(dsn) |
85 cur = dbh.cursor() | 98 cur = dbh.cursor() |
86 | 99 |