From 6e672c991187f383d950f0cf692af67486c506ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Fri, 8 Mar 2019 20:31:05 +0100 Subject: [PATCH] Add LogAdapter This adds the account to all log messages issues in module code --- gajim/common/modules/base.py | 5 ++++- gajim/common/modules/util.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/gajim/common/modules/base.py b/gajim/common/modules/base.py index 6607ea5ff..a8bc821bc 100644 --- a/gajim/common/modules/base.py +++ b/gajim/common/modules/base.py @@ -24,6 +24,7 @@ import nbxmpp from nbxmpp.structs import StanzaHandler from gajim.common import app +from gajim.common.modules.util import LogAdapter log = logging.getLogger('gajim.c.m.base') @@ -33,9 +34,11 @@ class BaseModule: _nbxmpp_extends = '' _nbxmpp_methods = [] # type: List[str] - def __init__(self, con): + def __init__(self, con, logger=None): self._con = con self._account = con.name + if logger is not None: + self._log = LogAdapter(logger, {'account': self._account}) self._nbxmpp_callbacks = {} # type: Dict[str, Any] self._stored_publish = None # type: Callable self.handlers = [] # type: List[str] diff --git a/gajim/common/modules/util.py b/gajim/common/modules/util.py index 5ff3891aa..7da2e8684 100644 --- a/gajim/common/modules/util.py +++ b/gajim/common/modules/util.py @@ -16,6 +16,7 @@ from typing import Union +from logging import LoggerAdapter from functools import wraps from functools import partial @@ -81,3 +82,8 @@ def get_eme_message(eme_data): return EME_MESSAGES[eme_data.namespace] except KeyError: return EME_MESSAGES['fallback'] % eme_data.name + + +class LogAdapter(LoggerAdapter): + def process(self, msg, kwargs): + return '[%s] %s' % (self.extra['account'], msg), kwargs