Skip to content

logging: add "only_once=False" param to logger.info/warning/etc calls #10015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions electrum/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,23 @@ def safe_translator(msg: str, **kwargs):
try:
parsed2 = list(sf.parse(translation))
except ValueError: # malformed format string in translation
_logger.info(f"rejected translation string: failed to parse. original={msg!r}. {translation=!r}")
_logger.warning(
f"rejected translation string: failed to parse. original={msg!r}. {translation=!r}",
only_once=True)
return msg
# num of replacement fields must match:
if len(parsed1) != len(parsed2):
_logger.info(f"rejected translation string: num replacement fields mismatch. original={msg!r}. {translation=!r}")
_logger.warning(
f"rejected translation string: num replacement fields mismatch. original={msg!r}. {translation=!r}",
only_once=True)
return msg
# set of "field_name"s must not change. (re-ordering is explicitly allowed):
field_names1 = set(tupl[1] for tupl in parsed1)
field_names2 = set(tupl[1] for tupl in parsed2)
if field_names1 != field_names2:
_logger.info(f"rejected translation string: set of field_names mismatch. original={msg!r}. {translation=!r}")
_logger.warning(
f"rejected translation string: set of field_names mismatch. original={msg!r}. {translation=!r}",
only_once=True)
return msg
# checks done.
return translation
Expand Down
23 changes: 21 additions & 2 deletions electrum/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import pathlib
import os
import platform
from typing import Optional, TYPE_CHECKING
from typing import Optional, TYPE_CHECKING, Set
import copy
import subprocess
import hashlib

if TYPE_CHECKING:
from .simple_config import SimpleConfig
Expand Down Expand Up @@ -190,6 +191,24 @@ def _process_verbosity_log_levels(verbosity):
raise Exception(f"invalid log filter: {filt}")


class _CustomLogger(logging.getLoggerClass()):
def __init__(self, name, *args, **kwargs):
super().__init__(name, *args, **kwargs)
self.msg_hashes_seen = set() # type: Set[bytes]
# ^ note: size grows without bounds, but only for log lines using "only_once".

def _log(self, level, msg: str, *args, only_once: bool = False, **kwargs) -> None:
"""Overridden to add 'only_once' arg to logger.debug()/logger.info()/logger.warning()/etc."""
if only_once: # if set, this logger will only log this msg a single time during its lifecycle
msg_hash = hashlib.sha256(msg.encode("utf-8")).digest()
if msg_hash in self.msg_hashes_seen:
return
self.msg_hashes_seen.add(msg_hash)
super()._log(level, msg, *args, **kwargs)

logging.setLoggerClass(_CustomLogger)


# enable logs universally (including for other libraries)
root_logger = logging.getLogger()
root_logger.setLevel(logging.WARNING)
Expand All @@ -216,7 +235,7 @@ def _process_verbosity_log_levels(verbosity):

# --- External API

def get_logger(name: str) -> logging.Logger:
def get_logger(name: str) -> _CustomLogger:
if name.startswith("electrum."):
name = name[9:]
return electrum_logger.getChild(name)
Expand Down