Skip to content

Commit

Permalink
:encoder_opts formatter option (#130)
Browse files Browse the repository at this point in the history
* Implemented `:encoder_opts` option for `LoggerJSON.Formatters.Basic`

* Added preliminary test for testing `:encoder_opts` on basic formatter

* Updated typespec for `t:LoggerJSON.Formatter.opts/0`

* Use project convention for typespecs

* Added `:encoder_opts` option to all formatters

* Added `:encoder_opts` to documentation

* Debrittle `:encoder_opts` tests

* Point to Jason docs for encoder options
  • Loading branch information
anthonator authored Aug 20, 2024
1 parent 2e72e28 commit 669631c
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/logger_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ defmodule LoggerJSON do
Some formatters require additional configuration options. Here are the options that are common for each formatter:
* `:encoder_opts` - options to be passed directly to the JSON encoder. This allows you to customize the behavior of the JSON
encoder. See the [documentation for Jason](https://hexdocs.pm/jason/Jason.html#encode/2-options) for available options. By
default, no options are passed to the encoder.
* `:metadata` - a list of metadata keys to include in the log entry. By default, no metadata is included.
If `:all`is given, all metadata is included. If `{:all_except, keys}` is given, all metadata except
the specified keys is included.
Expand Down
3 changes: 2 additions & 1 deletion lib/logger_json/formatter.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule LoggerJSON.Formatter do
@type opts :: [
{:metadata, :all | {:all_except, [atom()]} | [atom()]}
{:encoder_opts, [Jason.encode_opt()]}
| {:metadata, :all | {:all_except, [atom()]} | [atom()]}
| {:redactors, [{module(), term()}]}
| {atom(), term()}
]
Expand Down
3 changes: 2 additions & 1 deletion lib/logger_json/formatters/basic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule LoggerJSON.Formatters.Basic do
@impl true
def format(%{level: level, meta: meta, msg: msg}, opts) do
opts = Keyword.new(opts)
encoder_opts = Keyword.get(opts, :encoder_opts, [])
metadata_keys_or_selector = Keyword.get(opts, :metadata, [])
metadata_selector = update_metadata_selector(metadata_keys_or_selector, @processed_metadata_keys)
redactors = Keyword.get(opts, :redactors, [])
Expand All @@ -48,7 +49,7 @@ defmodule LoggerJSON.Formatters.Basic do
|> maybe_put(:request, format_http_request(meta))
|> maybe_put(:span, format_span(meta))
|> maybe_put(:trace, format_trace(meta))
|> Jason.encode_to_iodata!()
|> Jason.encode_to_iodata!(encoder_opts)

[line, "\n"]
end
Expand Down
3 changes: 2 additions & 1 deletion lib/logger_json/formatters/datadog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule LoggerJSON.Formatters.Datadog do
@impl true
def format(%{level: level, meta: meta, msg: msg}, opts) do
opts = Keyword.new(opts)
encoder_opts = Keyword.get(opts, :encoder_opts, [])
redactors = Keyword.get(opts, :redactors, [])
hostname = Keyword.get(opts, :hostname, :system)

Expand All @@ -77,7 +78,7 @@ defmodule LoggerJSON.Formatters.Datadog do
|> maybe_merge(format_http_request(meta))
|> maybe_merge(encode(metadata, redactors))
|> maybe_merge(encode(message, redactors))
|> Jason.encode_to_iodata!()
|> Jason.encode_to_iodata!(encoder_opts)

[line, "\n"]
end
Expand Down
3 changes: 2 additions & 1 deletion lib/logger_json/formatters/elastic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ defmodule LoggerJSON.Formatters.Elastic do
@impl LoggerJSON.Formatter
def format(%{level: level, meta: meta, msg: msg}, opts) do
opts = Keyword.new(opts)
encoder_opts = Keyword.get(opts, :encoder_opts, [])
metadata_keys_or_selector = Keyword.get(opts, :metadata, [])
metadata_selector = update_metadata_selector(metadata_keys_or_selector, @processed_metadata_keys)
redactors = Keyword.get(opts, :redactors, [])
Expand All @@ -167,7 +168,7 @@ defmodule LoggerJSON.Formatters.Elastic do
|> maybe_merge(format_http_request(meta))
|> maybe_put(:"span.id", format_span_id(meta))
|> maybe_put(:"trace.id", format_trace_id(meta))
|> Jason.encode_to_iodata!()
|> Jason.encode_to_iodata!(encoder_opts)

[line, "\n"]
end
Expand Down
3 changes: 2 additions & 1 deletion lib/logger_json/formatters/google_cloud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
@impl true
def format(%{level: level, meta: meta, msg: msg}, opts) do
opts = Keyword.new(opts)
encoder_opts = Keyword.get(opts, :encoder_opts, [])
redactors = Keyword.get(opts, :redactors, [])
service_context = Keyword.get_lazy(opts, :service_context, fn -> %{service: to_string(node())} end)
project_id = Keyword.get(opts, :project_id)
Expand Down Expand Up @@ -129,7 +130,7 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
|> maybe_put(:httpRequest, format_http_request(meta))
|> maybe_merge(encode(message, redactors))
|> maybe_merge(encode(metadata, redactors))
|> Jason.encode_to_iodata!()
|> Jason.encode_to_iodata!(encoder_opts)

[line, "\n"]
end
Expand Down
10 changes: 10 additions & 0 deletions test/logger_json/formatters/basic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,14 @@ defmodule LoggerJSON.Formatters.BasicTest do
}
}
end

test "passing options to encoder" do
formatter = {Basic, encoder_opts: [pretty: true]}
:logger.update_handler_config(:default, :formatter, formatter)

assert capture_log(fn ->
Logger.debug("Hello")
end) =~
~r/\n\s{2}"message": "Hello"/
end
end
10 changes: 10 additions & 0 deletions test/logger_json/formatters/datadog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,14 @@ defmodule LoggerJSON.Formatters.DatadogTest do
}
} = log_entry
end

test "passing options to encoder" do
formatter = {Datadog, encoder_opts: [pretty: true]}
:logger.update_handler_config(:default, :formatter, formatter)

assert capture_log(fn ->
Logger.debug("Hello")
end) =~
~r/\n\s{2}"message": "Hello"/
end
end
10 changes: 10 additions & 0 deletions test/logger_json/formatters/elastic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,14 @@ defmodule LoggerJSON.Formatters.ElasticTest do
assert String.starts_with?(origin_function, "test logs caught errors/1")
assert String.starts_with?(stacktrace, "** (RuntimeError) oops")
end

test "passing options to encoder" do
formatter = {Elastic, encoder_opts: [pretty: true]}
:logger.update_handler_config(:default, :formatter, formatter)

assert capture_log(fn ->
Logger.debug("Hello")
end) =~
~r/\n\s{2}"message": "Hello"/
end
end
10 changes: 10 additions & 0 deletions test/logger_json/formatters/google_cloud_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,14 @@ defmodule LoggerJSON.Formatters.GoogleCloudTest do
"serviceContext" => %{"service" => "nonode@nohost"}
} = log_entry
end

test "passing options to encoder" do
formatter = {GoogleCloud, encoder_opts: [pretty: true]}
:logger.update_handler_config(:default, :formatter, formatter)

assert capture_log(fn ->
Logger.debug("Hello")
end) =~
~r/\n\s{2}"message": "Hello"/
end
end

0 comments on commit 669631c

Please sign in to comment.