@@ -4,6 +4,10 @@ using LoggingExtras: FormatLogger
4
4
using LoggingFormats: LoggingFormats, Truncated, JSON, LogFmt
5
5
import JSON3
6
6
7
+ function my_throwing_function ()
8
+ throw (ArgumentError (" no" ))
9
+ end
10
+
7
11
@testset " Truncating" begin
8
12
@test LoggingFormats. shorten_str (" αβγαβγ" , 3 ) == " αβ…"
9
13
@test LoggingFormats. shorten_str (" αβγαβγ" , 4 ) == " αβγ…"
167
171
@warn " msg with \" quotes\" "
168
172
@error " error msg with nothings" _module= nothing _file= nothing __line= nothing
169
173
@error :notstring x = [1 , 2 , 3 ] y = " hello\" \" world"
174
+ @error " hi" exception= ErrorException (" bad" )
170
175
end
171
176
strs = collect (eachline (seekstart (io)))
172
177
@test occursin (" level=debug msg=\" debug msg\" module=Main" , strs[1 ])
183
188
@test occursin (" line=\" nothing\" " , strs[4 ])
184
189
@test occursin (" level=error msg=\" notstring\" module=Main" , strs[5 ])
185
190
@test occursin (" x=\" [1, 2, 3]\" y=\" hello\\\" \\\" world\" " , strs[5 ])
191
+ @test occursin (" exception=\" bad\" " , strs[6 ])
192
+
193
+ # Now let's try exceptions with backtraces
194
+ io = IOBuffer ()
195
+ with_logger (FormatLogger (LogFmt (), io)) do
196
+ try
197
+ my_throwing_function ()
198
+ catch e
199
+ @error " Oh no" exception = (e, catch_backtrace ())
200
+ end
201
+ end
202
+ str = String (take! (io))
203
+ @test occursin (" level=error msg=\" Oh no\" module=Main" , str)
204
+ @test occursin (" file=\" " , str)
205
+ @test occursin (" group=\" " , str)
206
+ @test occursin (" exception=\" ERROR: ArgumentError: no\\ nStacktrace:" , str)
207
+ # no new lines (except at the end of the line)
208
+ @test ! occursin (' \n ' , chomp (str))
209
+ # no Ptr's showing up in the backtrace
210
+ @test ! occursin (" Ptr" , str)
211
+ # Test we are getting at least some stacktrace, e.g., the function we called:
212
+ @test occursin (" my_throwing_function()" , str)
213
+
214
+ # Can test by converting to JSON with the node `logfmt` package:
215
+ # first install it with `npm i -g logfmt`
216
+ # Then:
217
+ # in = Base.PipeEndpoint()
218
+ # out = Base.PipeEndpoint()
219
+ # p = run(pipeline(`logfmt`; stdin=in, stdout=out); wait=false)
220
+ # write(in, str)
221
+ # close(in)
222
+ # wait(p)
223
+ # output = JSON3.read(read(out))
186
224
end
0 commit comments