@@ -120,3 +120,82 @@ func TestWithAdditionalLocationOffset(t *testing.T) {
120120 })
121121 }
122122}
123+
124+ func TestWithRootFields (t * testing.T ) {
125+ t .Parallel ()
126+
127+ testCases := map [string ]struct {
128+ logMessage string
129+ rootFields map [string ]interface {}
130+ subsystemFields map [string ]interface {}
131+ expectedOutput []map [string ]interface {}
132+ }{
133+ "no-root-log-fields" : {
134+ subsystemFields : map [string ]interface {}{
135+ "test-subsystem-key" : "test-subsystem-value" ,
136+ },
137+ logMessage : "test message" ,
138+ expectedOutput : []map [string ]interface {}{
139+ {
140+ "@level" : hclog .Trace .String (),
141+ "@message" : "test message" ,
142+ "@module" : testSubsystemModule ,
143+ "test-subsystem-key" : "test-subsystem-value" ,
144+ },
145+ },
146+ },
147+ "with-root-log-fields" : {
148+ subsystemFields : map [string ]interface {}{
149+ "test-subsystem-key" : "test-subsystem-value" ,
150+ },
151+ logMessage : "test message" ,
152+ rootFields : map [string ]interface {}{
153+ "test-root-key" : "test-root-value" ,
154+ },
155+ expectedOutput : []map [string ]interface {}{
156+ {
157+ "@level" : hclog .Trace .String (),
158+ "@message" : "test message" ,
159+ "@module" : testSubsystemModule ,
160+ "test-root-key" : "test-root-value" ,
161+ "test-subsystem-key" : "test-subsystem-value" ,
162+ },
163+ },
164+ },
165+ }
166+
167+ for name , testCase := range testCases {
168+ name , testCase := name , testCase
169+
170+ t .Run (name , func (t * testing.T ) {
171+ t .Parallel ()
172+
173+ var outputBuffer bytes.Buffer
174+
175+ ctx := context .Background ()
176+ ctx = loggertest .SDKRoot (ctx , & outputBuffer )
177+
178+ for key , value := range testCase .rootFields {
179+ ctx = tfsdklog .With (ctx , key , value )
180+ }
181+
182+ ctx = tfsdklog .NewSubsystem (ctx , testSubsystem , tfsdklog .WithRootFields ())
183+
184+ for key , value := range testCase .subsystemFields {
185+ ctx = tfsdklog .SubsystemWith (ctx , testSubsystem , key , value )
186+ }
187+
188+ tfsdklog .SubsystemTrace (ctx , testSubsystem , testCase .logMessage )
189+
190+ got , err := loggertest .MultilineJSONDecode (& outputBuffer )
191+
192+ if err != nil {
193+ t .Fatalf ("unable to read multiple line JSON: %s" , err )
194+ }
195+
196+ if diff := cmp .Diff (testCase .expectedOutput , got ); diff != "" {
197+ t .Errorf ("unexpected output difference: %s" , diff )
198+ }
199+ })
200+ }
201+ }
0 commit comments