@@ -56,37 +56,55 @@ lvlstr(lvl::Logging.LogLevel) = lvl >= Logging.Error ? "error" :
5656 lvl >= Logging. Info ? " info" :
5757 " debug"
5858
59- struct JSONLogMessage
59+ struct JSONLogMessage{T}
6060 level:: String
6161 msg:: String
6262 _module:: Union{String,Nothing}
6363 file:: Union{String,Nothing}
6464 line:: Union{Int,Nothing}
6565 group:: Union{String,Nothing}
6666 id:: Union{String,Nothing}
67- kwargs:: Dict{String,String }
67+ kwargs:: Dict{String,T }
6868end
69- function JSONLogMessage (args)
70- JSONLogMessage (
69+
70+ transform (:: Type{String} , v) = string (v)
71+ transform (:: Type{Any} , v) = v
72+
73+ function JSONLogMessage {T} (args) where {T}
74+ JSONLogMessage {T} (
7175 lvlstr (args. level),
7276 args. message isa AbstractString ? args. message : string (args. message),
7377 args. _module === nothing ? nothing : string (args. _module),
7478 args. file,
7579 args. line,
7680 args. group === nothing ? nothing : string (args. group),
7781 args. id === nothing ? nothing : string (args. id),
78- Dict {String,String } (string (k) => string ( v) for (k, v) in args. kwargs)
82+ Dict {String,T } (string (k) => transform (T, v) for (k, v) in args. kwargs)
7983 )
8084end
81- StructTypes. StructType (:: Type{JSONLogMessage} ) = StructTypes. OrderedStruct ()
82- StructTypes. names (:: Type{JSONLogMessage} ) = ((:_module , :module ), )
85+ StructTypes. StructType (:: Type{<: JSONLogMessage} ) = StructTypes. OrderedStruct ()
86+ StructTypes. names (:: Type{<: JSONLogMessage} ) = ((:_module , :module ), )
8387
8488struct JSON <: Function
89+ recursive:: Bool
8590end
8691
87- function (:: JSON )(io, args)
88- logmsg = JSONLogMessage (args)
89- JSON3. write (io, logmsg)
92+ JSON (; recursive= false ) = JSON (recursive)
93+
94+ function (j:: JSON )(io, args)
95+ if j. recursive
96+ logmsg = JSONLogMessage {Any} (args)
97+ try
98+ JSON3. write (io, logmsg)
99+ catch e
100+ fallback_msg = JSONLogMessage {String} (args)
101+ fallback_msg. kwargs[" LoggingFormats.FormatError" ] = sprint (Base. showerror, e)
102+ JSON3. write (io, fallback_msg)
103+ end
104+ else
105+ logmsg = JSONLogMessage {String} (args)
106+ JSON3. write (io, logmsg)
107+ end
90108 println (io)
91109 return nothing
92110end
0 commit comments