hampushellsberg has uploaded this change for review. (
https://gerrit.osmocom.org/c/pysim/+/44009?usp=email )
Change subject: log: print a record only once when loggers are nested
......................................................................
log: print a record only once when loggers are nested
PySimLogger.get() adds a handler to every logger it returns, and a
record also reaches the handlers of all parent loggers. As
pySim.global_platform and pySim.global_platform.scp both use it, every
SCP02/SCP03 log line was printed twice.
The folder osmocom-pysim-log-duplicate-handlers of
hampushellsberg-dev/PySIM-upstream-patches on GitHub has the analysis
and a repro.
Change-Id: Icdd68c493db51f3ea30a21f06b747f4e2782624b
---
M pySim/log.py
M tests/unittests/test_log.py
2 files changed, 13 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/09/44009/1
diff --git a/pySim/log.py b/pySim/log.py
index 5aeeed4..9ec47e5 100644
--- a/pySim/log.py
+++ b/pySim/log.py
@@ -41,6 +41,10 @@
self.log_callback = log_callback
def emit(self, record):
+ # a record also reaches the handlers PySimLogger.get() put on its
parent loggers
+ if getattr(record, '_pysim_printed', False):
+ return
+ record._pysim_printed = True
formatted_message = self.format(record)
self.log_callback(formatted_message, record)
diff --git a/tests/unittests/test_log.py b/tests/unittests/test_log.py
index a43fe41..3f1ea41 100755
--- a/tests/unittests/test_log.py
+++ b/tests/unittests/test_log.py
@@ -150,5 +150,14 @@
PySimLogger.setup(self._test_print_callback, {logging.WARN: YELLOW})
log.warning(TEST_MSG_WARNING) # don't leak cmd2
Color StrEnum
+ def test_06_nested_loggers(self):
+ # the record also reaches the handlers of the parent loggers
+ printed = []
+ PySimLogger.setup(printed.append)
+ PySimLogger.get(__name__ + '.nested')
+ child = PySimLogger.get(__name__ + '.nested.child')
+ child.warning(TEST_MSG_WARNING)
+ self.assertEqual([m.strip() for m in printed], ["WARNING: " +
TEST_MSG_WARNING])
+
if __name__ == '__main__':
unittest.main()
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/44009?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Icdd68c493db51f3ea30a21f06b747f4e2782624b
Gerrit-Change-Number: 44009
Gerrit-PatchSet: 1
Gerrit-Owner: hampushellsberg <[email protected]>