mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-31 17:31:36 +00:00
logging: port ugly accidental side-effect into readable explicit feature
LogFormatterForConsole.format() was mutating its 'record' argument, which was affecting all handlers/formatters
This commit is contained in:
parent
a3e522efd9
commit
387834164c
1 changed files with 20 additions and 9 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue