Skip to content

Commit cf26084

Browse files
committed
Add new/1 to Redactor
Closes #151
1 parent 92d21c8 commit cf26084

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

lib/logger_json/formatter.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ defmodule LoggerJSON.Formatter do
88

99
@type encoder_opts :: JSON.encoder() | [Jason.encode_opt()] | term()
1010

11-
@callback new(opts) :: {module, :logger.formatter_config()}
11+
@doc """
12+
Creates a new configuration for the formatter.
13+
"""
14+
@callback new(opts) :: {module, term()}
15+
16+
@doc """
17+
Formats a log event.
18+
"""
1219
@callback format(event :: :logger.log_event(), opts :: opts()) :: iodata()
1320

1421
@encoder Application.compile_env(:logger_json, :encoder, Jason)

lib/logger_json/redactor.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ defmodule LoggerJSON.Redactor do
66
For more information about encoding and redacting see `LoggerJSON.Formatter.RedactorEncoder.encode/2`.
77
"""
88

9+
@doc """
10+
Creates a new redactor.
11+
"""
12+
@callback new(opts :: term()) :: {module(), term()}
13+
914
@doc """
1015
Takes a key and a value and returns a redacted value.
1116
1217
This callback will be applied on key-value pairs, like elements of structs, maps or keyword lists.
1318
"""
1419
@callback redact(key :: String.t(), value :: term(), opts :: term()) :: term()
20+
21+
# TODO: Make it required in a future version
22+
@optional_callbacks new: 1
1523
end

lib/logger_json/redactors/redact_keys.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule LoggerJSON.Redactors.RedactKeys do
88
formatter:
99
LoggerJSON.Formatters.Basic.new(
1010
redactors: [
11-
{LoggerJSON.Redactors.RedactKeys, ["password"]}
11+
LoggerJSON.Redactors.RedactKeys.new(["password"])
1212
]
1313
)
1414
```
@@ -18,6 +18,12 @@ defmodule LoggerJSON.Redactors.RedactKeys do
1818

1919
@behaviour LoggerJSON.Redactor
2020

21+
@impl LoggerJSON.Redactor
22+
def new(keys) do
23+
{__MODULE__, keys}
24+
end
25+
26+
@impl LoggerJSON.Redactor
2127
def redact(key, value, keys) do
2228
if key in keys do
2329
"[REDACTED]"

test/logger_json/formatter/redactor_encoder_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule LoggerJSON.Formatter.RedactorEncoderTest do
88
defmodule PasswordStruct, do: defstruct(password: "foo")
99

1010
@encoder LoggerJSON.Formatter.encoder()
11-
@redactors [{LoggerJSON.Redactors.RedactKeys, ["password"]}]
11+
@redactors [LoggerJSON.Redactors.RedactKeys.new(["password"])]
1212

1313
describe "encode/2" do
1414
test "allows nils" do

0 commit comments

Comments
 (0)