Skip to content

Commit 9367c8b

Browse files
authored
handle exceptions in any key (#15)
1 parent 14d6f24 commit 9367c8b

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ julia> with_logger(FormatLogger(LoggingFormats.JSON(; recursive=true), stderr))
3737
```
3838

3939
If it encounters something which does not have a defined `StructTypes.StructType` to use
40-
for serializing to JSON, it will fallback to converting the objects to strings, like the default `recursive=false` option does. Handles the key `exception` specially, by printing errors and stacktraces using `Base.showerror`.
40+
for serializing to JSON (or otherwise errors when serializing to JSON), it will fallback to converting the objects to strings, like the default `recursive=false` option does. Handles exceptions specially, by printing errors and stacktraces using `Base.showerror`.
4141

4242
```julia
4343
julia> f() = try
@@ -66,7 +66,7 @@ level=info msg="hello, world" module=Main file="REPL[2]" line=2 group="REPL[2]"
6666
level=error msg="something is wrong" module=Main file="REPL[2]" line=3 group="REPL[2]" id=Main_2289c7f8
6767
```
6868

69-
Similarly to the JSON logger, `LogFmt` handles the key `exception` specially, by printing errors and stacktraces using `Base.showerror`.
69+
Similarly to the JSON logger, `LogFmt` handles exceptions specially, by printing errors and stacktraces using `Base.showerror`.
7070

7171
## `Truncated`: Truncate long variables and messages
7272

src/LoggingFormats.jl

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,9 @@ end
7070
transform(::Type{String}, v) = string(v)
7171
transform(::Type{Any}, v) = v
7272

73-
function maybe_stringify_exceptions(key, v)
74-
key == :exception || return v
75-
if v isa Tuple && length(v) == 2 && v[1] isa Exception
76-
e, bt = v
77-
msg = sprint(Base.display_error, e, bt)
78-
return msg
79-
end
80-
return sprint(showerror, v)
81-
end
82-
83-
# Use key information, then lower to 2-arg transform
84-
function transform(::Type{T}, key, v) where {T}
85-
v = maybe_stringify_exceptions(key, v)
86-
return transform(T, v)
87-
end
73+
maybe_stringify_exceptions((e, bt)::Tuple{Exception,Any}) = sprint(Base.display_error, e, bt)
74+
maybe_stringify_exceptions(e::Exception) = sprint(showerror, e)
75+
maybe_stringify_exceptions(v) = v
8876

8977
function JSONLogMessage{T}(args) where {T}
9078
JSONLogMessage{T}(
@@ -95,7 +83,7 @@ function JSONLogMessage{T}(args) where {T}
9583
args.line,
9684
args.group === nothing ? nothing : string(args.group),
9785
args.id === nothing ? nothing : string(args.id),
98-
Dict{String,T}(string(k) => transform(T, k, v) for (k, v) in args.kwargs)
86+
Dict{String,T}(string(k) => transform(T, maybe_stringify_exceptions(v)) for (k, v) in args.kwargs)
9987
)
10088
end
10189
StructTypes.StructType(::Type{<:JSONLogMessage}) = StructTypes.OrderedStruct()
@@ -152,7 +140,7 @@ function (::LogFmt)(io, args)
152140
)
153141
for (k, v) in args.kwargs
154142
print(io, " ", k, "=\"")
155-
v = maybe_stringify_exceptions(k, v)
143+
v = maybe_stringify_exceptions(v)
156144
escape_string(io, sprint(print, something(v, "nothing")), '"')
157145
print(io, "\"")
158146
end

test/runtests.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,21 @@ end
134134
# no stacktrace
135135
io = IOBuffer()
136136
with_logger(FormatLogger(JSON(; recursive=recursive), io)) do
137-
try
138-
throw(ArgumentError("no"))
139-
catch e
140-
@error "Oh no" exception = e
141-
end
137+
@error "Oh no" exception = ArgumentError("no")
142138
end
143139
logs = JSON3.read(seekstart(io))
144140
@test logs["msg"] == "Oh no"
145141
@test logs["kwargs"]["exception"] == "ArgumentError: no"
146142

143+
# non-standard exception key
144+
io = IOBuffer()
145+
with_logger(FormatLogger(JSON(; recursive=recursive), io)) do
146+
@error "Oh no" ex = ArgumentError("no")
147+
end
148+
logs = JSON3.read(seekstart(io))
149+
@test logs["msg"] == "Oh no"
150+
@test logs["kwargs"]["ex"] == "ArgumentError: no"
151+
147152
# stacktrace
148153
io = IOBuffer()
149154
with_logger(FormatLogger(JSON(; recursive=recursive), io)) do

0 commit comments

Comments
 (0)