33import warnings
44
55import pytest
6+ from crds .core import log as crds_log
67
78import stpipe .cmdline
89from stpipe import Step
@@ -49,7 +50,7 @@ def _clean_up_logging():
4950
5051
5152class LoggingStep (Step ):
52- """A Step that utilizes a local logger to log a warning ."""
53+ """A Step that uses a local and external logger to log messages ."""
5354
5455 spec = """
5556 str1 = string(default='default')
@@ -74,7 +75,7 @@ def get_stpipe_loggers():
7475
7576
7677class LoggingPipeline (Pipeline ):
77- """A Pipeline that utilizes a local logger to log a warning ."""
78+ """A Pipeline that uses a local and external logger to log messages ."""
7879
7980 spec = """
8081 str1 = string(default='default')
@@ -589,3 +590,56 @@ def get_stpipe_loggers():
589590 assert len (recwarn ) == 1 # Unchanged from above assert
590591 assert MSG in caplog .text
591592 assert logging ._warnings_showwarning is None
593+
594+
595+ @pytest .mark .parametrize ("restore_console" , [True , False ])
596+ def test_crds_log (capsys , restore_console ):
597+ class CRDSLoggingStep (LoggingStep ):
598+ def process (self ):
599+ logger .info (STEP_INFO )
600+ logger .warning (STEP_WARNING )
601+ logger .debug (STEP_DEBUG )
602+ crds_log .info (EXTERNAL_INFO )
603+ crds_log .warning (EXTERNAL_WARNING )
604+ crds_log .debug (EXTERNAL_DEBUG )
605+
606+ @staticmethod
607+ def get_stpipe_loggers ():
608+ return ("stpipe" , "CRDS" )
609+
610+ crds_logger = logging .getLogger ("CRDS" )
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
621+
622+ # During the step call, the CRDS handler is removed if necessary and
623+ # the stpipe handler is attached
624+ CRDSLoggingStep .call ()
625+
626+ # With default configuration, exactly one info and warning message is expected
627+ # from each captured logger in the stderr stream
628+ capt = capsys .readouterr ()
629+ for msg in ALL_MESSAGES :
630+ assert capt .out == ""
631+ if msg in [STEP_INFO , STEP_WARNING , EXTERNAL_INFO , EXTERNAL_WARNING ]:
632+ assert msg in capt .err
633+ assert capt .err .count (msg ) == 1
634+ else :
635+ assert capt .err .count (msg ) == 0
636+
637+ # After the call is complete, the stpipe logger is removed and the CRDS
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