refactor: use f-string in logging where it is easy

% is only interpreted if there are two or more arguments:
<https://docs.python.org/3/library/logging.html#logging.Logger.debug>
So it is safe to pass a single argument with already formatted
string.
This commit is contained in:
link2xt
2024-07-15 19:37:03 +00:00
parent d3c29b2f6e
commit e8bf051cd0
6 changed files with 14 additions and 14 deletions

View File

@@ -233,7 +233,7 @@ def handle_dovecot_protocol(rfile, wfile, db: Database, config: Config):
try: try:
res = handle_dovecot_request(msg, db, config) res = handle_dovecot_request(msg, db, config)
except UnknownCommand: except UnknownCommand:
logging.warning("unknown command: %r", msg) logging.warning(f"unknown command: {msg!r}")
else: else:
if res: if res:
wfile.write(res.encode("ascii")) wfile.write(res.encode("ascii"))

View File

@@ -21,9 +21,9 @@ hooks = events.HookCollection()
@hooks.on(events.RawEvent) @hooks.on(events.RawEvent)
def log_event(event): def log_event(event):
if event.kind == EventType.INFO: if event.kind == EventType.INFO:
logging.info("%s", event.msg) logging.info(event.msg)
elif event.kind == EventType.WARNING: elif event.kind == EventType.WARNING:
logging.warning("%s", event.msg) logging.warning(event.msg)
@hooks.on(events.RawEvent(EventType.ERROR)) @hooks.on(events.RawEvent(EventType.ERROR))
@@ -45,7 +45,7 @@ def on_group_image_changed(event):
@hooks.on(events.GroupNameChanged) @hooks.on(events.GroupNameChanged)
def on_group_name_changed(event): def on_group_name_changed(event):
logging.info("group name changed, old name: %s", event.old_name) logging.info(f"group name changed, old name: {event.old_name}")
@hooks.on(events.NewMessage(func=lambda e: not e.command)) @hooks.on(events.NewMessage(func=lambda e: not e.command))
@@ -72,7 +72,7 @@ def main():
with Rpc() as rpc: with Rpc() as rpc:
deltachat = DeltaChat(rpc) deltachat = DeltaChat(rpc)
system_info = deltachat.get_system_info() system_info = deltachat.get_system_info()
logging.info("Running deltachat core %s", system_info.deltachat_core_version) logging.info(f"Running deltachat core {system_info.deltachat_core_version}")
accounts = deltachat.get_all_accounts() accounts = deltachat.get_all_accounts()
account = accounts[0] if accounts else deltachat.add_account() account = accounts[0] if accounts else deltachat.add_account()

View File

@@ -32,5 +32,5 @@ class FileDict:
except FileNotFoundError: except FileNotFoundError:
return {} return {}
except Exception: except Exception:
logging.warning("corrupt serialization state at: %r", self.path) logging.warning(f"corrupt serialization state at: {self.path!r}")
return {} return {}

View File

@@ -152,7 +152,7 @@ class BeforeQueueHandler:
self.send_rate_limiter = SendRateLimiter() self.send_rate_limiter = SendRateLimiter()
async def handle_MAIL(self, server, session, envelope, address, mail_options): async def handle_MAIL(self, server, session, envelope, address, mail_options):
logging.info("handle_MAIL from %s", address) logging.info(f"handle_MAIL from {address}")
envelope.mail_from = address envelope.mail_from = address
max_sent = self.config.max_user_send_per_minute max_sent = self.config.max_user_send_per_minute
if not self.send_rate_limiter.is_sending_allowed(address, max_sent): if not self.send_rate_limiter.is_sending_allowed(address, max_sent):
@@ -176,13 +176,13 @@ class BeforeQueueHandler:
def check_DATA(self, envelope): def check_DATA(self, envelope):
"""the central filtering function for e-mails.""" """the central filtering function for e-mails."""
logging.info("Processing DATA message from %s", envelope.mail_from) logging.info(f"Processing DATA message from {envelope.mail_from}")
message = BytesParser(policy=policy.default).parsebytes(envelope.content) message = BytesParser(policy=policy.default).parsebytes(envelope.content)
mail_encrypted = check_encrypted(message) mail_encrypted = check_encrypted(message)
_, from_addr = parseaddr(message.get("from").strip()) _, from_addr = parseaddr(message.get("from").strip())
logging.info("mime-from: %s envelope-from: %r", from_addr, envelope.mail_from) logging.info(f"mime-from: {from_addr} envelope-from: {envelope.mail_from!r}")
if envelope.mail_from.lower() != from_addr.lower(): if envelope.mail_from.lower() != from_addr.lower():
return f"500 Invalid FROM <{from_addr!r}> for <{envelope.mail_from!r}>" return f"500 Invalid FROM <{from_addr!r}> for <{envelope.mail_from!r}>"

View File

@@ -82,7 +82,7 @@ def handle_dovecot_request(msg, transactions, notifier, metadata, iroh_relay=Non
): ):
# Handle `GETMETADATA "" /shared/vendor/deltachat/irohrelay` # Handle `GETMETADATA "" /shared/vendor/deltachat/irohrelay`
return f"O{iroh_relay}\n" return f"O{iroh_relay}\n"
logging.warning("lookup ignored: %r", msg) logging.warning(f"lookup ignored: {msg!r}")
return "N\n" return "N\n"
elif short_command == DICTPROXY_ITERATE_CHAR: elif short_command == DICTPROXY_ITERATE_CHAR:
# Empty line means ITER_FINISHED. # Empty line means ITER_FINISHED.
@@ -92,7 +92,7 @@ def handle_dovecot_request(msg, transactions, notifier, metadata, iroh_relay=Non
return # no version checking return # no version checking
if short_command not in (DICTPROXY_TRANSACTION_CHARS): if short_command not in (DICTPROXY_TRANSACTION_CHARS):
logging.warning("unknown dictproxy request: %r", msg) logging.warning(f"unknown dictproxy request: {msg!r}")
return return
transaction_id = parts[0] transaction_id = parts[0]

View File

@@ -92,7 +92,7 @@ class Notifier:
def requeue_persistent_queue_items(self): def requeue_persistent_queue_items(self):
for queue_path in self.queue_dir.iterdir(): for queue_path in self.queue_dir.iterdir():
if queue_path.name.endswith(".tmp"): if queue_path.name.endswith(".tmp"):
logging.warning("removing spurious queue item: %r", queue_path) logging.warning(f"removing spurious queue item: {queue_path!r}")
queue_path.unlink() queue_path.unlink()
continue continue
queue_item = PersistentQueueItem.read_from_path(queue_path) queue_item = PersistentQueueItem.read_from_path(queue_path)
@@ -104,7 +104,7 @@ class Notifier:
deadline = queue_item.start_ts + self.DROP_DEADLINE deadline = queue_item.start_ts + self.DROP_DEADLINE
if retry_num >= len(self.retry_queues) or when > deadline: if retry_num >= len(self.retry_queues) or when > deadline:
queue_item.delete() queue_item.delete()
logging.error("notification exceeded deadline: %r", queue_item.token) logging.error(f"notification exceeded deadline: {queue_item.token!r}")
return return
self.retry_queues[retry_num].put((when, queue_item)) self.retry_queues[retry_num].put((when, queue_item))
@@ -162,5 +162,5 @@ class NotifyThread(Thread):
queue_item.delete() queue_item.delete()
return return
logging.warning("Notification request failed: %r", res) logging.warning(f"Notification request failed: {res!r}")
self.notifier.queue_for_retry(queue_item, retry_num=self.retry_num + 1) self.notifier.queue_for_retry(queue_item, retry_num=self.retry_num + 1)