Skip to content

Commit

Permalink
add recursive option to JSON log format (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericphanson authored Nov 18, 2021
1 parent 6546262 commit 57dd263
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "LoggingFormats"
uuid = "98105f81-4425-4516-93fd-1664fb551ab6"
version = "1.0.0"
version = "1.1.0"

[deps]
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ julia> with_logger(FormatLogger(LoggingFormats.JSON(), stderr)) do
{"level":"error","msg":"something is wrong","module":"Main","file":"REPL[10]","line":3,"group":"REPL[10]","id":"Main_2289c7f9","kwargs":{}}
```

One can also pass `recursive=true` to recursively serialize the `kwargs` as JSON:

```julia
julia> using LoggingFormats, LoggingExtras

julia> with_logger(FormatLogger(LoggingFormats.JSON(; recursive=true), stderr)) do
@info "hello, world" key=Dict("hello" => true)
end
{"level":"info","msg":"hello, world","module":"Main","file":"REPL[18]","line":2,"group":"REPL[18]","id":"Main_ffce16b5","kwargs":{"key":{"hello":true}}}
```

If it encounters something which does not have a defined `StructTypes.StructType` to use
for serializing to JSON, it will fallback to converting the objects to strings, like the default `recursive=false` option does.

## `LogFmt`: Format log events as logfmt

`LoggingFormats.LogFmt()` is a function which formats the log message in the
Expand Down
38 changes: 28 additions & 10 deletions src/LoggingFormats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,55 @@ lvlstr(lvl::Logging.LogLevel) = lvl >= Logging.Error ? "error" :
lvl >= Logging.Info ? "info" :
"debug"

struct JSONLogMessage
struct JSONLogMessage{T}
level::String
msg::String
_module::Union{String,Nothing}
file::Union{String,Nothing}
line::Union{Int,Nothing}
group::Union{String,Nothing}
id::Union{String,Nothing}
kwargs::Dict{String,String}
kwargs::Dict{String,T}
end
function JSONLogMessage(args)
JSONLogMessage(

transform(::Type{String}, v) = string(v)
transform(::Type{Any}, v) = v

function JSONLogMessage{T}(args) where {T}
JSONLogMessage{T}(
lvlstr(args.level),
args.message isa AbstractString ? args.message : string(args.message),
args._module === nothing ? nothing : string(args._module),
args.file,
args.line,
args.group === nothing ? nothing : string(args.group),
args.id === nothing ? nothing : string(args.id),
Dict{String,String}(string(k) => string(v) for (k, v) in args.kwargs)
Dict{String,T}(string(k) => transform(T, v) for (k, v) in args.kwargs)
)
end
StructTypes.StructType(::Type{JSONLogMessage}) = StructTypes.OrderedStruct()
StructTypes.names(::Type{JSONLogMessage}) = ((:_module, :module), )
StructTypes.StructType(::Type{<:JSONLogMessage}) = StructTypes.OrderedStruct()
StructTypes.names(::Type{<:JSONLogMessage}) = ((:_module, :module), )

struct JSON <: Function
recursive::Bool
end

function (::JSON)(io, args)
logmsg = JSONLogMessage(args)
JSON3.write(io, logmsg)
JSON(; recursive=false) = JSON(recursive)

function (j::JSON)(io, args)
if j.recursive
logmsg = JSONLogMessage{Any}(args)
try
JSON3.write(io, logmsg)
catch e
fallback_msg = JSONLogMessage{String}(args)
fallback_msg.kwargs["LoggingFormats.FormatError"] = sprint(Base.showerror, e)
JSON3.write(io, fallback_msg)
end
else
logmsg = JSONLogMessage{String}(args)
JSON3.write(io, logmsg)
end
println(io)
return nothing
end
Expand Down
36 changes: 36 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import JSON3
@test occursin("│ short_var = a", str)
end

struct NoStructTypeDefined
f::Int
end

@testset "JSON" begin
@test LoggingFormats.lvlstr(Logging.Error + 1) == "error"
@test LoggingFormats.lvlstr(Logging.Error) == "error"
Expand Down Expand Up @@ -93,6 +97,38 @@ end
@test json.line isa Int
@test json.kwargs.x == "[1, 2, 3]"
@test json.kwargs.y == "(1, 2)"

# `recursive=true`
io = IOBuffer()
with_logger(FormatLogger(JSON(; recursive=true), io)) do
y = (1, 2)
@info "info msg" x = [1, 2, 3] y = Dict("hi" => Dict("hi2" => [1,2]))
end
json = JSON3.read(seekstart(io))
@test json.level == "info"
@test json.msg == "info msg"
@test json.module == "Main"
@test json.line isa Int
@test json.kwargs.x == [1, 2, 3]
@test json.kwargs.y == Dict(:hi => Dict(:hi2 => [1,2]))

# Fallback to strings
io = IOBuffer()
with_logger(FormatLogger(JSON(; recursive=true), io)) do
y = (1, 2)
@info "info msg" x = [1, 2, 3] y = Dict("hi" => NoStructTypeDefined(1))
end
json = JSON3.read(seekstart(io))
@test json.level == "info"
@test json.msg == "info msg"
@test json.module == "Main"
@test json.line isa Int
@test json.kwargs.x == "[1, 2, 3]"
y = json.kwargs.y
must_have = ("Dict", "\"hi\"", "=>", "NoStructTypeDefined(1)")
@test all(h -> occursin(h, y), must_have) # avoid issues with printing changing with versions
@test json.kwargs[Symbol("LoggingFormats.FormatError")] == "ArgumentError: NoStructTypeDefined doesn't have a defined `StructTypes.StructType`"

end

@testset "logfmt" begin
Expand Down

2 comments on commit 57dd263

@fredrikekre
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/49006

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.0 -m "<description of version>" 57dd263cd231160fb0e2c2f2e5607e319b4b8929
git push origin v1.1.0

Please sign in to comment.