22from unittest import mock
33
44import dagster as dg
5- from dagster ._core .definitions .instigation_logger import InstigationLogger
5+ from dagster ._core .definitions .instigation_logger import InstigationLogger , get_instigation_log_records
66from dagster ._core .storage .noop_compute_log_manager import NoOpComputeLogManager
77
88
@@ -74,3 +74,42 @@ def test_instigation_logger_log_failure(capsys):
7474 captured .err .count ("Exception closing logger write stream: Exception: OOPS ON EXIT" )
7575 == 1
7676 )
77+
78+
79+ def test_instigation_logger_logs_weird_record_ok (capsys ):
80+ class NonJsonSerializable :
81+ """A class that json.dumps doesn't know what to do with (by default)."""
82+ def __str__ (self ):
83+ return "non-jsonable-object"
84+
85+ log_key = ["test-repo" , "test-instigator" , "123" ]
86+
87+ with dg .instance_for_test () as instance : # pyright: ignore[reportAttributeAccessIssue]
88+ with InstigationLogger (
89+ log_key = log_key ,
90+ instance = instance ,
91+ repository_name = "test-repo" ,
92+ instigator_name = "test-instigator" ,
93+ ) as logger :
94+ logger .info ("log without extras" )
95+ logger .info ("log with extras" , extra = {"non_json" : NonJsonSerializable ()})
96+
97+ assert logger .has_captured_logs ()
98+
99+ captured = capsys .readouterr ()
100+
101+ assert "Exception initializing logger write stream" not in captured .err
102+ assert "Exception writing to logger event stream" not in captured .err
103+ assert "Exception closing logger write stream" not in captured .err
104+ assert "NonJsonSerializable is not JSON serializable" not in captured .err
105+
106+ records = get_instigation_log_records (instance , log_key )
107+
108+ assert len (records ) >= 2
109+ assert "test-repo - test-instigator - log with extras" in {
110+ record .get ("msg" ) for record in records if record .get ("msg" )
111+ }
112+ matching_records = [record for record in records if record .get ("non_json" )]
113+ assert matching_records and matching_records [0 ]["non_json" ] == "non-jsonable-object"
114+
115+ instance .compute_log_manager .delete_logs (log_key = log_key )
0 commit comments