diff --git a/CHANGES.rst b/CHANGES.rst index 58ae8969..4c16a813 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,7 @@ 0.6.1 (unreleased) ================== -- log only strings in ``Step.log_records`` when a formatter is provided [#171] + +- 0.6.0 (2024-01-24) ================== diff --git a/src/stpipe/log.py b/src/stpipe/log.py index 8c55bf77..b3fc93b7 100644 --- a/src/stpipe/log.py +++ b/src/stpipe/log.py @@ -280,23 +280,18 @@ def log_records(self): return self._log_records def emit(self, record): - if self.formatter is not None: - self._log_records.append(self.formatter.format(record)) + self._log_records.append(record) @contextmanager -def record_logs(level=logging.NOTSET, formatter=None): - if formatter is None: - yield [] - else: - handler = RecordingHandler(level=level) - handler.setFormatter(formatter) - logger = getLogger(STPIPE_ROOT_LOGGER) - logger.addHandler(handler) - try: - yield handler.log_records - finally: - logger.removeHandler(handler) +def record_logs(level=logging.NOTSET): + handler = RecordingHandler(level=level) + logger = getLogger(STPIPE_ROOT_LOGGER) + logger.addHandler(handler) + try: + yield handler.log_records + finally: + logger.removeHandler(handler) # Install the delegation handler on the root logger. The Step class diff --git a/src/stpipe/step.py b/src/stpipe/step.py index ad6dbac0..6ebe27ea 100644 --- a/src/stpipe/step.py +++ b/src/stpipe/step.py @@ -74,10 +74,6 @@ class Step: # but by default attempt to prefetch prefetch_references = True - # This needs to be set to a logging formatter for any - # log_records to be saved. - _log_records_formatter = None - @classmethod def get_config_reftype(cls): """ @@ -408,7 +404,7 @@ def log_records(self): Returns ------- - list of str + list of logging.LogRecord """ return self._log_records @@ -420,7 +416,7 @@ def run(self, *args): """ gc.collect() - with log.record_logs(formatter=self._log_records_formatter) as log_records: + with log.record_logs() as log_records: self._log_records = log_records # Make generic log messages go to this step's logger diff --git a/tests/test_logger.py b/tests/test_logger.py index a8e7bed5..ccaf013e 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -53,9 +53,7 @@ def test_record_logs(): isinstance(h, stpipe_log.RecordingHandler) for h in root_logger.handlers ) - with stpipe_log.record_logs( - level=logging.ERROR, formatter=logging.Formatter("%(message)s") - ) as log_records: + with stpipe_log.record_logs(level=logging.ERROR) as log_records: stpipe_logger.warning("Warning from stpipe") stpipe_logger.error("Error from stpipe") root_logger.warning("Warning from root") @@ -69,5 +67,5 @@ def test_record_logs(): root_logger.error("Additional error from root") assert len(log_records) == 2 - assert log_records[0] == "Error from stpipe" - assert log_records[1] == "Error from root" + assert log_records[0].message == "Error from stpipe" + assert log_records[1].message == "Error from root" diff --git a/tests/test_step.py b/tests/test_step.py index 972f84a9..f271a999 100644 --- a/tests/test_step.py +++ b/tests/test_step.py @@ -51,7 +51,6 @@ class LoggingPipeline(Pipeline): str1 = string(default='default') output_ext = string(default='simplestep') """ - _log_records_formatter = logging.Formatter("%(message)s") def process(self): self.log.warning("This step has called out a warning.") @@ -410,4 +409,6 @@ def test_log_records(): pipeline = LoggingPipeline() pipeline.run() - assert any(r == "This step has called out a warning." for r in pipeline.log_records) + assert any( + r.message == "This step has called out a warning." for r in pipeline.log_records + )