Skip to content

Commit 6517a37

Browse files
committed
Inline crds log interface instead and check for previous console log
1 parent 2f1139d commit 6517a37

3 files changed

Lines changed: 35 additions & 30 deletions

File tree

src/stpipe/crds_client.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
This module defines functions that connect the core crds package
33
to stpipe, tailoring it to provide results in the forms required
44
by stpipe.
5-
6-
WARNING: stpipe and crds have circular dependencies. Do not use crds imports
7-
directly in modules other than this crds_client so that dependency order and
8-
general integration can be managed here.
95
"""
106

117
import re
@@ -26,16 +22,6 @@
2622
]
2723

2824

29-
def remove_crds_log_handler():
30-
"""Remove the default stream handler for the CRDS logger."""
31-
log.remove_console_handler()
32-
33-
34-
def restore_crds_log_handler():
35-
"""Restore the default stream handler for the CRDS logger."""
36-
log.add_console_handler()
37-
38-
3925
def get_multiple_reference_paths(parameters, reference_file_types, observatory):
4026
"""Aligns stpipe requirements with CRDS library top level interfaces.
4127

src/stpipe/log.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
from astropy.extern.configobj import validate
1414
from astropy.extern.configobj.configobj import ConfigObj
15+
from crds.core import log as crds_log
1516

16-
from . import config_parser, crds_client
17+
from . import config_parser
1718

1819
STPIPE_ROOT_LOGGER = "stpipe"
1920
DEFAULT_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
@@ -102,6 +103,7 @@ def __init__(
102103
self.handlers.append(BreakHandler(self.break_level))
103104

104105
self._previous_level = {}
106+
self._previous_crds_console = False
105107

106108
def get_handler(self, handler_str):
107109
"""
@@ -136,7 +138,7 @@ def apply(self, log_names=None):
136138
- "py.warnings": If provided, Python warnings are captured and
137139
logged (``logging.captureWarnings(True)``).
138140
- "CRDS": If provided, the default stream handler for the CRDS
139-
logger is removed.
141+
logger is removed if needed.
140142
141143
Parameters
142144
----------
@@ -149,7 +151,9 @@ def apply(self, log_names=None):
149151
if "py.warnings" in log_names:
150152
logging.captureWarnings(True)
151153
if "CRDS" in log_names:
152-
crds_client.remove_crds_log_handler()
154+
self._previous_crds_console = crds_log.THE_LOGGER.console is not None
155+
if self._previous_crds_console:
156+
crds_log.remove_console_handler()
153157
for log_name in log_names:
154158
# Don't reapply configuration, to avoid overwriting the
155159
# previously recorded level.
@@ -173,8 +177,9 @@ def undo(self, log_names=None, close_handlers=True):
173177
174178
- "py.warnings": If provided, Python warnings are no longer captured
175179
and logged (``logging.captureWarnings(False)``).
176-
- "CRDS": If provided, the default stream handler for the CRDS
177-
logger is restored.
180+
- "CRDS": If provided and if the default stream handler for the CRDS
181+
logger was present when configuration was applied, it will be
182+
restored.
178183
179184
Parameters
180185
----------
@@ -207,8 +212,9 @@ def undo(self, log_names=None, close_handlers=True):
207212
LogConfig.applied = None
208213
if "py.warnings" in log_names:
209214
logging.captureWarnings(False)
210-
if "CRDS" in log_names:
211-
crds_client.restore_crds_log_handler()
215+
if "CRDS" in log_names and self._previous_crds_console:
216+
self._previous_crds_console = False
217+
crds_log.add_console_handler()
212218

213219
@contextmanager
214220
def context(self, log_names=None):

tests/test_logger.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import warnings
44

55
import pytest
6+
from crds.core import log as crds_log
67

78
import stpipe.cmdline
89
from stpipe import Step
910
from stpipe import log as stpipe_log
10-
from stpipe.crds_client import log as crds_log
1111
from stpipe.pipeline import Pipeline
1212

1313

@@ -592,7 +592,8 @@ def get_stpipe_loggers():
592592
assert logging._warnings_showwarning is None
593593

594594

595-
def test_crds_log(capsys):
595+
@pytest.mark.parametrize("restore_console", [True, False])
596+
def test_crds_log(capsys, restore_console):
596597
class CRDSLoggingStep(LoggingStep):
597598
def process(self):
598599
logger.info(STEP_INFO)
@@ -606,12 +607,19 @@ def process(self):
606607
def get_stpipe_loggers():
607608
return ("stpipe", "CRDS")
608609

609-
# Before the step call, the crds logger has a stream handler
610610
crds_logger = logging.getLogger("CRDS")
611-
assert len(crds_logger.handlers) == 1
612-
assert isinstance(crds_logger.handlers[0], logging.StreamHandler)
611+
if restore_console:
612+
# Before the step call, the crds logger has a stream handler
613+
assert len(crds_logger.handlers) == 1
614+
assert isinstance(crds_logger.handlers[0], logging.StreamHandler)
615+
else:
616+
# remove any current console handler
617+
crds_log.remove_console_handler()
618+
619+
# Before the step call, the crds logger has no handlers
620+
assert len(crds_logger.handlers) == 0
613621

614-
# During the step call, the CRDS handler is removed and
622+
# During the step call, the CRDS handler is removed if necessary and
615623
# the stpipe handler is attached
616624
CRDSLoggingStep.call()
617625

@@ -627,6 +635,11 @@ def get_stpipe_loggers():
627635
assert capt.err.count(msg) == 0
628636

629637
# After the call is complete, the stpipe logger is removed and the CRDS
630-
# stream logger is restored
631-
assert len(crds_logger.handlers) == 1
632-
assert isinstance(crds_logger.handlers[0], logging.StreamHandler)
638+
# stream logger is restored if it was previously present
639+
if restore_console:
640+
assert len(crds_logger.handlers) == 1
641+
assert isinstance(crds_logger.handlers[0], logging.StreamHandler)
642+
else:
643+
assert len(crds_logger.handlers) == 0
644+
# Clean up: restore a console logger
645+
crds_log.add_console_handler()

0 commit comments

Comments
 (0)