9898 @test json. kwargs. x == " [1, 2, 3]"
9999 @test json. kwargs. y == " (1, 2)"
100100
101- # `recursive=true`
101+
102+ # nest_kwargs=false
102103 io = IOBuffer ()
103- with_logger (FormatLogger (JSON (; recursive = true ), io)) do
104+ with_logger (FormatLogger (JSON (; nest_kwargs = false ), io)) do
104105 y = (1 , 2 )
105- @info " info msg" x = [1 , 2 , 3 ] y = Dict ( " hi " => Dict ( " hi2 " => [ 1 , 2 ]))
106+ @info " info msg" x = [1 , 2 , 3 ] y
106107 end
107108 json = JSON3. read (seekstart (io))
108109 @test json. level == " info"
109110 @test json. msg == " info msg"
110111 @test json. module == " Main"
111112 @test json. line isa Int
112- @test json. kwargs. x == [1 , 2 , 3 ]
113- @test json. kwargs. y == Dict (:hi => Dict (:hi2 => [1 ,2 ]))
113+ # not tucked under `kwargs`
114+ @test json. x == " [1, 2, 3]"
115+ @test json. y == " (1, 2)"
114116
115- # Fallback to strings
117+
118+ # With clash
119+ io = IOBuffer ()
120+ with_logger (FormatLogger (JSON (; nest_kwargs= false ), io)) do
121+ @info " info msg" line = [1 , 2 , 3 ]
122+ end
123+ json = JSON3. read (seekstart (io))
124+ @test json. level == " info"
125+ @test json. msg == " info msg"
126+ @test json. module == " Main"
127+ @test json. line isa Int
128+ # key was renamed to prevent clash:
129+ @test json. _line == " [1, 2, 3]"
130+
131+ # `recursive=true`
116132 io = IOBuffer ()
117133 with_logger (FormatLogger (JSON (; recursive= true ), io)) do
118- y = (1 , 2 )
119- @info " info msg" x = [1 , 2 , 3 ] y = Dict (" hi" => NaN )
134+ @info " info msg" x = [1 , 2 , 3 ] y = Dict (" hi" => Dict (" hi2" => [1 ,2 ]))
120135 end
121136 json = JSON3. read (seekstart (io))
122137 @test json. level == " info"
123138 @test json. msg == " info msg"
124139 @test json. module == " Main"
125140 @test json. line isa Int
126- @test json. kwargs. x == " [1, 2, 3]"
127- y = json. kwargs. y
128- must_have = (" Dict" , " \" hi\" " , " =>" , " NaN" )
129- @test all (h -> occursin (h, y), must_have) # avoid issues with printing changing with versions
130- @test json. kwargs[Symbol (" LoggingFormats.FormatError" )] == " NaN not allowed to be written in JSON spec"
141+ @test json. kwargs. x == [1 , 2 , 3 ]
142+ @test json. kwargs. y == Dict (:hi => Dict (:hi2 => [1 ,2 ]))
143+
144+ # Fallback to strings
145+ for nest_kwargs in (true , false )
146+ io = IOBuffer ()
147+ with_logger (FormatLogger (JSON (; recursive= true , nest_kwargs= nest_kwargs), io)) do
148+ @info " info msg" x = [1 , 2 , 3 ] y = Dict (" hi" => NaN )
149+ end
150+ json = JSON3. read (seekstart (io))
151+ @test json. level == " info"
152+ @test json. msg == " info msg"
153+ @test json. module == " Main"
154+ @test json. line isa Int
155+ if nest_kwargs
156+ @test json. kwargs. x == " [1, 2, 3]"
157+ @test json. kwargs[Symbol (" LoggingFormats.FormatError" )] == " NaN not allowed to be written in JSON spec"
158+ y = json. kwargs. y
159+ else
160+ @test json. x == " [1, 2, 3]"
161+ @test json[Symbol (" LoggingFormats.FormatError" )] == " NaN not allowed to be written in JSON spec"
162+ y = json. y
163+ end
164+ must_have = (" Dict" , " \" hi\" " , " =>" , " NaN" )
165+ @test all (h -> occursin (h, y), must_have) # avoid issues with printing changing with versions
166+ end
131167
132168 # Test logging exceptions
133- for recursive in (false , true )
169+ for recursive in (false , true ), nest_kwargs in ( true , false )
134170 # no stacktrace
135171 io = IOBuffer ()
136- with_logger (FormatLogger (JSON (; recursive= recursive), io)) do
172+ with_logger (FormatLogger (JSON (; recursive= recursive, nest_kwargs = nest_kwargs ), io)) do
137173 @error " Oh no" exception = ArgumentError (" no" )
138174 end
139175 logs = JSON3. read (seekstart (io))
140176 @test logs[" msg" ] == " Oh no"
141- @test logs[" kwargs" ][" exception" ] == " ArgumentError: no"
177+ ex = nest_kwargs ? logs[" kwargs" ][" exception" ] : logs[" exception" ]
178+ @test ex == " ArgumentError: no"
142179
143180 # non-standard exception key
144181 io = IOBuffer ()
145- with_logger (FormatLogger (JSON (; recursive= recursive), io)) do
182+ with_logger (FormatLogger (JSON (; recursive= recursive, nest_kwargs = nest_kwargs ), io)) do
146183 @error " Oh no" ex = ArgumentError (" no" )
147184 end
148185 logs = JSON3. read (seekstart (io))
149186 @test logs[" msg" ] == " Oh no"
150- @test logs[" kwargs" ][" ex" ] == " ArgumentError: no"
187+ ex = nest_kwargs ? logs[" kwargs" ][" ex" ] : logs[" ex" ]
188+ @test ex == " ArgumentError: no"
151189
152190 # stacktrace
153191 io = IOBuffer ()
154- with_logger (FormatLogger (JSON (; recursive= recursive), io)) do
192+ with_logger (FormatLogger (JSON (; recursive= recursive, nest_kwargs = nest_kwargs ), io)) do
155193 try
156194 throw (ArgumentError (" no" ))
157195 catch e
@@ -161,10 +199,10 @@ end
161199 logs = JSON3. read (seekstart (io))
162200 @test logs[" msg" ] == " Oh no"
163201
164- @test occursin (" ArgumentError: no" , logs[" kwargs" ][" exception" ])
202+ ex = nest_kwargs ? logs[" kwargs" ][" exception" ] : logs[" exception" ]
203+ @test occursin (" ArgumentError: no" , ex)
165204 # Make sure we get a stacktrace out:
166- @test occursin (r" ArgumentError: no\n Stacktrace:\s * \[ 1\] " ,
167- logs[" kwargs" ][" exception" ])
205+ @test occursin (r" ArgumentError: no\n Stacktrace:\s * \[ 1\] " , ex)
168206 end
169207end
170208
0 commit comments