diff --git a/ansible_mitogen/logging.py b/ansible_mitogen/logging.py index 76b42c7b..071ecd10 100644 --- a/ansible_mitogen/logging.py +++ b/ansible_mitogen/logging.py @@ -46,6 +46,9 @@ class Handler(logging.Handler): self.normal_method = normal_method def emit(self, record): + if getattr(record, 'mitogen_name', '') == 'stderr': + record.levelno = logging.ERROR + s = '[pid %d] %s' % (os.getpid(), self.format(record)) if record.levelno >= logging.ERROR: self.display.error(s, wrap_text=False) diff --git a/docs/getting_started.rst b/docs/getting_started.rst index 21bc0c35..3b50e757 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -125,6 +125,19 @@ Logging Environment Variables logs of any IO interaction, which is useful when debugging deadlocks. + +Logging Records +~~~~~~~~~~~~~~~ + +Messages received from a child context via :class:`mitogen.master.LogForwarder` +receive extra attributes: + +* `mitogen_context`: :class:`mitogen.master.Context` referring to the message + source. +* `mitogen_name`: original logger name in the source context. +* `mitogen_msg`: original message in the source context. + + Creating A Context ------------------ diff --git a/mitogen/master.py b/mitogen/master.py index 1e46a916..d0bc55a8 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -309,7 +309,11 @@ class LogForwarder(object): self._cache[msg.src_id] = logger = logging.getLogger(name) name, level_s, s = msg.data.split('\x00', 2) - logger.log(int(level_s), '%s: %s', name, s) + logger.log(int(level_s), '%s: %s', name, s, extra={ + 'mitogen_message': s, + 'mitogen_context': self._router.context_by_id(msg.src_id), + 'mitogen_name': name, + }) def __repr__(self): return 'LogForwarder(%r)' % (self._router,)