Skip to content

Commit

Permalink
Extra fields configuration to optionally log host and query_params
Browse files Browse the repository at this point in the history
  • Loading branch information
navinpeiris committed Jul 27, 2023
1 parent 0288f68 commit 352d2d0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 2.0.0-rc.2
# 2.0.0-rc.3

- `extra_fields` configuration to optionally enable `host` and `query` params being logged.
- `extra_fields` configuration to optionally enable `host` and `query_params` params being logged.

# 2.0.0-rc.1

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ def log_level(_), do: :info
One or more of the following fields can be optionally enabled through the `extra_fields` configuration option:

- host
- query
- query_params

Example:

```elixir
config :logster, extra_fields: [:host, :query]
config :logster, extra_fields: [:host, :query_params]
```

### Renaming default fields
Expand Down
6 changes: 3 additions & 3 deletions lib/logster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,15 @@ defmodule Logster do
end

defp maybe_put_query_params(fields, conn) do
if extra_fields() |> Enum.member?(:query) do
if extra_fields() |> Enum.member?(:query_params) do
fields |> do_put_query_params(conn)
else
fields
end
end

defp do_put_query_params(fields, %Plug.Conn{query_params: %Plug.Conn.Unfetched{}}),
do: fields |> Keyword.put(:query, "[UNFETCHED]")
do: fields |> Keyword.put(:query_params, "[UNFETCHED]")

defp do_put_query_params(fields, %Plug.Conn{query_params: query_params}),
do: do_put_query_params(fields, query_params)
Expand All @@ -311,7 +311,7 @@ defmodule Logster do
|> filter_values()
|> format_values()

fields |> Keyword.put(:query, query_params)
fields |> Keyword.put(:query_params, query_params)
end

# convenience method to put the params at the end of the given list
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Logster.MixProject do
def project do
[
app: :logster,
version: "2.0.0-rc.2",
version: "2.0.0-rc.3",
name: "Logster",
description:
"Easily parsable single-line plain text and JSON logger for Plug and Phoenix applications",
Expand Down
16 changes: 8 additions & 8 deletions test/logster_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -766,28 +766,28 @@ defmodule Logster.Test do
assert {:params, %{"v" => "ok…ok"}} in fields
end

test "does not add query if not enabled in config" do
test "does not add query params if not enabled in config" do
fields =
%Plug.Conn{
query_params: %{"foo" => "bar"}
}
|> Logster.get_conn_fields()

assert fields |> Keyword.has_key?(:query) == false
assert fields |> Keyword.has_key?(:query_params) == false
end

@tag with_config: [extra_fields: [:query]]
@tag with_config: [extra_fields: [:query_params]]
test "sets query params as unfetched when enabled in config" do
fields =
%Plug.Conn{
query_params: %Plug.Conn.Unfetched{}
}
|> Logster.get_conn_fields()

assert {:query, "[UNFETCHED]"} in fields
assert {:query_params, "[UNFETCHED]"} in fields
end

@tag with_config: [extra_fields: [:query]]
@tag with_config: [extra_fields: [:query_params]]
test "adds query params when enabled in config" do
fields =
%Plug.Conn{
Expand All @@ -798,14 +798,14 @@ defmodule Logster.Test do
}
|> Logster.get_conn_fields()

assert {:query,
assert {:query_params,
%{
"one" => "two",
"foo" => "bar"
}} in fields
end

@tag with_config: [extra_fields: [:query]]
@tag with_config: [extra_fields: [:query_params]]
test "filters password query params by default" do
fields =
%Plug.Conn{
Expand All @@ -817,7 +817,7 @@ defmodule Logster.Test do
}
|> Logster.get_conn_fields()

assert {:query,
assert {:query_params,
%{
"one" => "two",
"password" => "[FILTERED]",
Expand Down

0 comments on commit 352d2d0

Please sign in to comment.