Skip to content

Commit

Permalink
add protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Dec 24, 2023
1 parent 36e3109 commit 0927541
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
18 changes: 16 additions & 2 deletions lib/ex_json_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ defmodule ExJsonLogger do
| metadata(metadata)
])
rescue
_e ->
e ->
IO.inspect(e)

Check warning on line 51 in lib/ex_json_logger.ex

View workflow job for this annotation

GitHub Actions / test (25, 1.15)

There should be no calls to IO.inspect/1.

Check warning on line 51 in lib/ex_json_logger.ex

View workflow job for this annotation

GitHub Actions / test (25, 1.14)

There should be no calls to IO.inspect/1.

Check warning on line 51 in lib/ex_json_logger.ex

View workflow job for this annotation

GitHub Actions / test (24, 1.15)

There should be no calls to IO.inspect/1.

Check warning on line 51 in lib/ex_json_logger.ex

View workflow job for this annotation

GitHub Actions / test (24, 1.14)

There should be no calls to IO.inspect/1.

Check warning on line 51 in lib/ex_json_logger.ex

View workflow job for this annotation

GitHub Actions / test (26, 1.15)

There should be no calls to IO.inspect/1.

Check warning on line 51 in lib/ex_json_logger.ex

View workflow job for this annotation

GitHub Actions / test (26, 1.14)

There should be no calls to IO.inspect/1.

encode([
{"level", "error"},
{"time", format_timestamp(timestamp)},
Expand Down Expand Up @@ -82,6 +84,10 @@ defmodule ExJsonLogger do

defp format_metadata({drop, _}) when drop in [:msg, :time, :level, :report_cb, :gl], do: nil

defp format_metadata({_, nil} = kv), do: kv
defp format_metadata({_, string} = kv) when is_binary(string), do: kv
defp format_metadata({_, number} = kv) when is_number(number), do: kv

defp format_metadata({key, pid}) when is_pid(pid) do
{key, unsafe_fragment(["#PID", :erlang.pid_to_list(pid)])}
end
Expand Down Expand Up @@ -113,5 +119,13 @@ defmodule ExJsonLogger do
{list_key, List.to_string(list)}
end

defp format_metadata({_k, _v} = other), do: other
defp format_metadata({k, %_struct{} = v} = kv) do
cond do
impl = String.Chars.impl_for(v) -> {k, impl.to_string(v)}
Jason.Encoder.impl_for(v) != Jason.Encoder.Any -> kv
true -> {k, inspect(v)}
end
end

defp format_metadata(other), do: other
end
4 changes: 4 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule ExJsonLogger.Mixfile do
name: "ex_json_logger",
version: @version,
elixir: ">= 1.14.0",
elixirc_paths: elixirc_paths(Mix.env()),
elixirc_options: [warnings_as_errors: true],
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
Expand All @@ -34,6 +35,9 @@ defmodule ExJsonLogger.Mixfile do
[extra_applications: [:logger]]
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_env), do: ["lib"]

defp deps do
[
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
Expand Down
43 changes: 36 additions & 7 deletions test/ex_json_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,7 @@ defmodule ExJsonLoggerTest do
Logger.debug("this is a message", level: :error, msg: "this is metadata")
end)

assert %{
"level" => "debug",
"msg" => "this is a message",
"time" => time
} = Jason.decode!(message)

refute String.starts_with?(time, "1970")
assert %{"level" => "debug", "msg" => "this is a message"} = Jason.decode!(message)
end

test "metadata can override :time" do
Expand All @@ -134,4 +128,39 @@ defmodule ExJsonLoggerTest do
assert %{"time" => "1970" <> _} = Jason.decode!(message)
end
end

describe "protocols" do
test "String.Chars" do
Logger.configure_backend(:console, metadata: :all)

message =
capture_log(fn ->
Logger.debug("this is a message", some_key: %StringCharsValue{message: "hello"})
end)

assert %{"some_key" => "StringCharsValue says: hello"} = Jason.decode!(message)
end

test "Jason.Encoder" do
Logger.configure_backend(:console, metadata: :all)

message =
capture_log(fn ->
Logger.debug("this is a message", some_key: %JasonEncoderValue{message: "hello", secret: "password"})
end)

assert %{"some_key" => %{"message" => "hello"}} = Jason.decode!(message)
end

test "Inspect" do
Logger.configure_backend(:console, metadata: :all)

message =
capture_log(fn ->
Logger.debug("this is a message", some_key: %InspectValue{message: "hello", secret: "password"})
end)

assert %{"some_key" => "#InspectValue<message: \"hello\", ...>"} = Jason.decode!(message)
end
end
end
19 changes: 19 additions & 0 deletions test/support/protocols.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule StringCharsValue do

Check warning on line 1 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (25, 1.15)

Modules should have a @moduledoc tag.

Check warning on line 1 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (25, 1.14)

Modules should have a @moduledoc tag.

Check warning on line 1 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (24, 1.15)

Modules should have a @moduledoc tag.

Check warning on line 1 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (24, 1.14)

Modules should have a @moduledoc tag.

Check warning on line 1 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (26, 1.15)

Modules should have a @moduledoc tag.

Check warning on line 1 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (26, 1.14)

Modules should have a @moduledoc tag.
defstruct [:message]

defimpl String.Chars do
def to_string(%{message: message}) do

Check warning on line 5 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (25, 1.15)

Functions should have a @SPEC type specification.

Check warning on line 5 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (25, 1.14)

Functions should have a @SPEC type specification.

Check warning on line 5 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (24, 1.15)

Functions should have a @SPEC type specification.

Check warning on line 5 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (24, 1.14)

Functions should have a @SPEC type specification.

Check warning on line 5 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (26, 1.15)

Functions should have a @SPEC type specification.

Check warning on line 5 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (26, 1.14)

Functions should have a @SPEC type specification.
"StringCharsValue says: " <> message
end
end
end

defmodule JasonEncoderValue do

Check warning on line 11 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (25, 1.15)

Modules should have a @moduledoc tag.

Check warning on line 11 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (25, 1.14)

Modules should have a @moduledoc tag.

Check warning on line 11 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (24, 1.15)

Modules should have a @moduledoc tag.

Check warning on line 11 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (24, 1.14)

Modules should have a @moduledoc tag.

Check warning on line 11 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (26, 1.15)

Modules should have a @moduledoc tag.

Check warning on line 11 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (26, 1.14)

Modules should have a @moduledoc tag.
@derive {Jason.Encoder, only: [:message]}
defstruct [:message, :secret]
end

defmodule InspectValue do

Check warning on line 16 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (25, 1.15)

Modules should have a @moduledoc tag.

Check warning on line 16 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (25, 1.14)

Modules should have a @moduledoc tag.

Check warning on line 16 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (24, 1.15)

Modules should have a @moduledoc tag.

Check warning on line 16 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (24, 1.14)

Modules should have a @moduledoc tag.

Check warning on line 16 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (26, 1.15)

Modules should have a @moduledoc tag.

Check warning on line 16 in test/support/protocols.ex

View workflow job for this annotation

GitHub Actions / test (26, 1.14)

Modules should have a @moduledoc tag.
@derive {Inspect, only: [:message]}
defstruct [:message, :secret]
end

0 comments on commit 0927541

Please sign in to comment.