Skip to content

Commit

Permalink
merge meta into body
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Nov 27, 2024
1 parent f9c276f commit aaaf2c9
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions lib/plausible/logger/json_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ defmodule Plausible.Logger.JSONFormatter do
try do
msg = process_message(msg, truncate)
meta = process_meta(meta, metadata_keys)

# TODO skip meta if empty?
# instead of {"level":"debug","time":"2024-11-26 12:46:36.600","msg":"hello","meta":{}}
# do {"level":"debug","time":"2024-11-26 12:46:36.600","msg":"hello"}
log = %Log{fields: [{"level", level}, {"time", time}, {"msg", msg}, {"meta", meta}]}
log = %Log{fields: [{"level", level}, {"time", time}, {"msg", msg} | meta]}
Jason.encode_to_iodata!(log)
catch
kind, reason ->
Expand Down Expand Up @@ -95,15 +91,10 @@ defmodule Plausible.Logger.JSONFormatter do
end

defp process_meta(meta, keys) do
meta_fields =
case keys do
:all -> meta |> Map.to_list() |> process_meta_all()
_keys when is_list(keys) -> process_meta_keys(keys, meta)
end

%Log{fields: meta_fields}
|> Jason.encode_to_iodata!()
|> Jason.Fragment.new()
case keys do
:all -> meta |> Map.to_list() |> process_meta_all()
_keys when is_list(keys) -> process_meta_keys(keys, meta)
end
end

defp process_meta_all([{k, v} | rest]) do
Expand Down Expand Up @@ -137,6 +128,8 @@ defmodule Plausible.Logger.JSONFormatter do
end

defp metadata(:time, _), do: nil
defp metadata(:level, _), do: nil
defp metadata(:msg, _), do: nil
defp metadata(:gl, _), do: nil
defp metadata(:report_cb, _), do: nil
defp metadata(_, nil), do: nil
Expand Down

0 comments on commit aaaf2c9

Please sign in to comment.