Improve logging

This commit is contained in:
s3lph 2022-04-19 01:17:25 +02:00
parent d50ce036ea
commit 6c9856c74a
5 changed files with 22 additions and 26 deletions

View file

@ -28,7 +28,6 @@ class KeyConflictResolution:
self._key_template: str = key_template
self._user_template: str = user_template
self._dry_run: bool = False
self._logger: logging.Logger = logging.getLogger()
def dry_run(self):
self._dry_run = True
@ -84,10 +83,10 @@ class KeyConflictResolution:
# Conflict Resolution: Choose the OLDEST subscription with a key, but notify using ALL keys
earliest: SchleuderSubscriber = min(notnull, key=lambda x: x.created_at)
assert earliest.key is not None # Make mypy happy; it can't know that earliest.key can't be None
self._logger.debug(f'Key Conflict for {earliest.email} in lists, chose {earliest.key.fingerprint}:')
logging.debug(f'Key Conflict for {earliest.email} in lists, chose {earliest.key.fingerprint}:')
for s in subscriptions:
fpr = 'no key' if s.key is None else s.key.fingerprint
self._logger.debug(f' - {s.schleuder}: {fpr}')
logging.debug(f' {s.schleuder}: {fpr}')
# Generate a SHA1 digest that only changes when the subscription list changes
digest = self._make_key_digest(earliest, subscriptions)
msg: Optional[Message] = None
@ -121,9 +120,10 @@ class KeyConflictResolution:
# Conflict Resolution: Choose the OLDEST subscription with a key, but notify using ALL recipients
earliest: SchleuderSubscriber = min(notnull, key=lambda x: x.created_at)
assert earliest.key is not None # Make mypy happy; it can't know that earliest.key can't be None
self._logger.debug(f'User Conflict for {earliest.key.fingerprint} in lists, chose {earliest.email}:')
logging.debug(f'User Conflict for {earliest.key.fingerprint} in lists, chose {earliest.email}:')
for s in subscriptions:
self._logger.debug(f' - {s.schleuder}: {s.email}')
sschleuder = sourcemap.get(s.schleuder, 'unknown')
logging.debug(f' {sschleuder}: {s.email}')
# Generate a SHA1 digest that only changes when the subscription list changes
digest = self._make_user_digest(earliest, subscriptions)
msgs: Optional[List[Message]] = None
@ -155,7 +155,7 @@ class KeyConflictResolution:
try:
state = json.load(f)
except BaseException:
self._logger.exception('Cannot read statefile. Not sending any messages!')
logging.exception('Cannot read statefile. Not sending any messages!')
return False
# Remove all state entries older than conflict_interval
state = {k: v for k, v in state.items() if now-v < self._interval}
@ -171,7 +171,7 @@ class KeyConflictResolution:
json.dump(state, f)
return send
except BaseException:
self._logger.exception('Cannot open or write statefile. Not sending any messages!')
logging.exception('Cannot open or write statefile. Not sending any messages!')
return False
def _make_key_digest(self, chosen: SchleuderSubscriber, candidates: List[SchleuderSubscriber]) -> str:

View file

@ -69,10 +69,9 @@ def main():
ap.add_argument('--verbose', '-v', action='store_true', default=False)
ap.add_argument('--version', action='version', version=__version__)
ns = ap.parse_args(sys.argv[1:])
if ns.verbose:
logger = logging.getLogger()
logger.setLevel('DEBUG')
logger.debug('Verbose logging enabled')
level: str = logging.DEBUG if ns.verbose else logging.INFO
logging.basicConfig(style='{', format='{asctime:s} [{levelname:^8s}] {message:}', level=level)
logging.debug('Verbose logging enabled')
lists, smtp = parse_config(ns)
for lst in lists:
lst.process(ns.dry_run)

View file

@ -28,10 +28,9 @@ class MultiList:
self._api: SchleuderApi = api
self._kcr: KeyConflictResolution = kcr
self._reporter: Reporter = reporter
self._logger: logging.Logger = logging.getLogger()
def process(self, dry_run: bool = False):
self._logger.info(f'Processing: {self._target} {"DRY RUN" if dry_run else ""}')
logging.info(f'Processing: {self._target} {"DRY RUN" if dry_run else ""}')
target_list, sources = self._lists_by_name()
target_admins = self._api.get_list_admins(target_list)
# Get current subs, except for unmanaged adresses
@ -67,29 +66,29 @@ class MultiList:
# Perform the actual list modifications in an order which avoids race conditions
for key in to_add:
self._api.post_key(key, target_list)
self._logger.info(f'Added key: {key}')
logging.info(f'Added key: {key}')
for sub in to_subscribe:
self._api.subscribe(sub, target_list)
self._logger.info(f'Subscribed user: {sub}')
logging.info(f'Subscribed user: {sub}')
for sub in to_update:
self._api.update_fingerprint(sub, target_list)
self._logger.info(f'Updated key: {sub}')
logging.info(f'Updated key: {sub}')
for sub in to_unsubscribe:
self._api.unsubscribe(sub, target_list)
self._logger.info(f'Unsubscribed user: {sub}')
logging.info(f'Unsubscribed user: {sub}')
for key in to_remove:
self._api.delete_key(key, target_list)
self._logger.info(f'Removed key: {key}')
logging.info(f'Removed key: {key}')
if len(to_add) + len(to_subscribe) + len(to_unsubscribe) + len(to_remove) == 0:
self._logger.info(f'No changes for {self._target}')
logging.info(f'No changes for {self._target}')
else:
for admin in target_admins:
report = AdminReport(self._target, admin.email, self._mail_from,
admin.key.blob if admin.key is not None else None,
to_subscribe, to_unsubscribe, to_update, to_add, to_remove)
self._reporter.add_message(report)
self._logger.info(f'Finished processing: {self._target}')
logging.info(f'Finished processing: {self._target}')
def _lists_by_name(self) -> Tuple[SchleuderList, List[SchleuderList]]:
lists = self._api.get_lists()

View file

@ -224,7 +224,6 @@ class AdminReport(Message):
class Reporter:
_messages: List['Message'] = []
_logger: logging.Logger = logging.getLogger()
def __init__(self,
send_conflict_messages: bool,

View file

@ -42,7 +42,6 @@ class SmtpClient:
self._password: Optional[str] = password
self._smtp: Optional[smtplib.SMTP] = None
self._dry_run: bool = False
self._logger = logging.getLogger()
@staticmethod
def parse(config: Dict[str, Any]) -> 'SmtpClient':
@ -63,7 +62,7 @@ class SmtpClient:
with self as smtp:
for m in messages:
msg = m.mime
self._logger.debug(f'MIME Message:\n{str(msg)}')
logging.debug(f'MIME Message:\n{str(msg)}')
self._send_message(msg)
def _send_message(self, msg: email.message.Message):
@ -71,7 +70,7 @@ class SmtpClient:
raise RuntimeError('SMTP connection is not established')
if not self._dry_run:
self._smtp.send_message(msg)
self._logger.debug(f'Sent email message.')
logging.debug(f'Sent email message.')
def __enter__(self):
# TLS from the start requires a different class
@ -84,7 +83,7 @@ class SmtpClient:
# Only sign in if both username and password are provided
if self._username is not None and self._password is not None:
smtp.login(self._username, self._password)
self._logger.debug(f'SMTP connection to {str(self)} established')
logging.debug(f'SMTP connection to {str(self)} established')
return smtp
def __exit__(self, exc_type, exc_val, exc_tb):
@ -92,7 +91,7 @@ class SmtpClient:
raise RuntimeError('SMTP connection is not established')
self._smtp.quit()
ret = self._smtp.__exit__(exc_type, exc_val, exc_tb)
self._logger.debug(f'SMTP connection to {str(self)} closed')
logging.debug(f'SMTP connection to {str(self)} closed')
self._smtp = None
return ret