@@ -31,7 +31,13 @@ def log_dir(tmpdir_factory):
3131 return str (tmpdir_factory .mktemp ('logs' ))
3232
3333
34- def test_bad_logging_launch_config ():
34+ @pytest .fixture
35+ def mock_clean_env (monkeypatch ):
36+ monkeypatch .delenv ('OVERRIDE_LAUNCH_SCREEN_FORMAT' , raising = False )
37+ monkeypatch .delenv ('OVERRIDE_LAUNCH_LOG_FORMAT' , raising = False )
38+
39+
40+ def test_bad_logging_launch_config (mock_clean_env ):
3541 """Tests that setup throws at bad configuration."""
3642 launch .logging .reset ()
3743
@@ -83,7 +89,7 @@ def test_output_loggers_bad_configuration(log_dir):
8389 },
8490 )
8591])
86- def test_output_loggers_configuration (capsys , log_dir , config , checks ):
92+ def test_output_loggers_configuration (capsys , log_dir , config , checks , mock_clean_env ):
8793 checks = {'stdout' : set (), 'stderr' : set (), 'both' : set (), ** checks }
8894 launch .logging .reset ()
8995 launch .logging .launch_config .log_dir = log_dir
@@ -162,7 +168,7 @@ def test_output_loggers_configuration(capsys, log_dir, config, checks):
162168 assert (not os .path .exists (own_log_path ) or 0 == os .stat (own_log_path ).st_size )
163169
164170
165- def test_screen_default_format_with_timestamps (capsys , log_dir ):
171+ def test_screen_default_format_with_timestamps (capsys , log_dir , mock_clean_env ):
166172 """Test screen logging when using the default logs format with timestamps."""
167173 launch .logging .reset ()
168174 launch .logging .launch_config .level = logging .DEBUG
@@ -181,7 +187,7 @@ def test_screen_default_format_with_timestamps(capsys, log_dir):
181187 assert 0 == len (capture .err )
182188
183189
184- def test_screen_default_format (capsys ):
190+ def test_screen_default_format (capsys , mock_clean_env ):
185191 """Test screen logging when using the default logs format."""
186192 launch .logging .reset ()
187193
@@ -218,6 +224,25 @@ def test_log_default_format(log_dir):
218224 assert re .match (r'[0-9]+\.[0-9]+ \[ERROR\] \[some-proc\]: baz' , lines [0 ]) is not None
219225
220226
227+ def test_logging_env_var_format (capsys , monkeypatch ):
228+ monkeypatch .setenv ('OVERRIDE_LAUNCH_SCREEN_FORMAT' , 'TESTSCREEN {message} {name} TESTSCREEN' )
229+ monkeypatch .setenv ('OVERRIDE_LAUNCH_LOG_FORMAT' , 'TESTLOG {message} {name} TESTLOG' )
230+ launch .logging .reset ()
231+
232+ logger = launch .logging .get_logger ('some-proc' )
233+ logger .addHandler (launch .logging .launch_config .get_screen_handler ())
234+
235+ logger .info ('bar' )
236+ capture = capsys .readouterr ()
237+ lines = capture .out .splitlines ()
238+ assert 'TESTSCREEN bar some-proc TESTSCREEN' == lines .pop ()
239+
240+ launch .logging .launch_config .get_log_file_handler ().flush ()
241+ with open (launch .logging .launch_config .get_log_file_path (), 'r' ) as f :
242+ lines = f .readlines ()
243+ assert 'TESTLOG bar some-proc TESTLOG\n ' == lines [0 ]
244+
245+
221246def test_log_handler_factory (log_dir ):
222247 """Test logging using a custom log handlers."""
223248 class TestStreamHandler (launch .logging .handlers .Handler ):
0 commit comments