commit 387834164cb28a0853d98f609491bab74d1c0975
parent a3e522efd968686a32cbf715e4ac91f531c5deca
Author: SomberNight <somber.night@protonmail.com>
Date: Thu, 2 May 2019 16:05:26 +0200
logging: port ugly accidental side-effect into readable explicit feature
LogFormatterForConsole.format() was mutating its 'record' argument,
which was affecting all handlers/formatters
Diffstat:
1 file changed, 20 insertions(+), 9 deletions(-)
diff --git a/electrum/logging.py b/electrum/logging.py
@@ -9,6 +9,7 @@ import pathlib
import os
import platform
from typing import Optional
+import copy
class LogFormatterForFiles(logging.Formatter):
@@ -20,6 +21,10 @@ class LogFormatterForFiles(logging.Formatter):
datefmt = "%Y%m%dT%H%M%S.%fZ"
return date.strftime(datefmt)
+ def format(self, record):
+ record = _shorten_name_of_logrecord(record)
+ return super().format(record)
+
file_formatter = LogFormatterForFiles(fmt="%(asctime)22s | %(levelname)8s | %(name)s | %(message)s")
@@ -27,15 +32,7 @@ file_formatter = LogFormatterForFiles(fmt="%(asctime)22s | %(levelname)8s | %(na
class LogFormatterForConsole(logging.Formatter):
def format(self, record):
- # strip the main module name from the logger name
- if record.name.startswith("electrum."):
- record.name = record.name[9:]
- # manual map to shorten common module names
- record.name = record.name.replace("interface.Interface", "interface", 1)
- record.name = record.name.replace("network.Network", "network", 1)
- record.name = record.name.replace("synchronizer.Synchronizer", "synchronizer", 1)
- record.name = record.name.replace("verifier.SPV", "verifier", 1)
- record.name = record.name.replace("gui.qt.main_window.ElectrumWindow", "gui.qt.main_window", 1)
+ record = _shorten_name_of_logrecord(record)
return super().format(record)
@@ -43,6 +40,20 @@ class LogFormatterForConsole(logging.Formatter):
console_formatter = LogFormatterForConsole(fmt="%(levelname).1s | %(name)s | %(message)s")
+def _shorten_name_of_logrecord(record: logging.LogRecord) -> logging.LogRecord:
+ record = copy.copy(record) # avoid mutating arg
+ # strip the main module name from the logger name
+ if record.name.startswith("electrum."):
+ record.name = record.name[9:]
+ # manual map to shorten common module names
+ record.name = record.name.replace("interface.Interface", "interface", 1)
+ record.name = record.name.replace("network.Network", "network", 1)
+ record.name = record.name.replace("synchronizer.Synchronizer", "synchronizer", 1)
+ record.name = record.name.replace("verifier.SPV", "verifier", 1)
+ record.name = record.name.replace("gui.qt.main_window.ElectrumWindow", "gui.qt.main_window", 1)
+ return record
+
+
# enable logs universally (including for other libraries)
root_logger = logging.getLogger()
root_logger.setLevel(logging.WARNING)