Skip to content

Commit

Permalink
Polish docs
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDryga committed May 14, 2024
1 parent a8dd04e commit e31100d
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/logger_json/formatter/redactor_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule LoggerJSON.Formatter.RedactorEncoder do
def encode(list, redactors) when is_list(list), do: for(el <- list, do: encode(el, redactors))
def encode(data, _redactors), do: inspect(data, pretty: true, width: 80)

def encode_key_value({key, value}, redactors) do
defp encode_key_value({key, value}, redactors) do
key = encode_key(key)
{key, encode(redact(key, value, redactors), redactors)}
end
Expand All @@ -75,7 +75,7 @@ defmodule LoggerJSON.Formatter.RedactorEncoder do

defp redact(key, value, redactors) do
Enum.reduce(redactors, value, fn {redactor, opts}, acc ->
redactor.redact(key, acc, opts)
redactor.redact(to_string(key), acc, opts)
end)
end
end
3 changes: 3 additions & 0 deletions lib/logger_json/formatters/basic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ defmodule LoggerJSON.Formatters.Basic do
[line, "\n"]
end

@doc false
def format_binary_message(binary) do
IO.chardata_to_string(binary)
end

@doc false
def format_structured_message(map) when is_map(map) do
map
end
Expand All @@ -64,6 +66,7 @@ defmodule LoggerJSON.Formatters.Basic do
Enum.into(keyword, %{})
end

@doc false
def format_crash_reason(binary, _reason, _meta) do
IO.chardata_to_string(binary)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/logger_json/formatters/datadog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ defmodule LoggerJSON.Formatters.Datadog do
[line, "\n"]
end

@doc false
def format_binary_message(binary) do
%{message: IO.chardata_to_string(binary)}
end

@doc false
def format_structured_message(map) when is_map(map) do
%{message: map}
end
Expand All @@ -93,6 +95,7 @@ defmodule LoggerJSON.Formatters.Datadog do
%{message: Enum.into(keyword, %{})}
end

@doc false
def format_crash_reason(binary, {%{} = _exception, stacktrace}, _meta) do
%{
error: %{
Expand Down
5 changes: 4 additions & 1 deletion lib/logger_json/formatters/google_cloud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
You can extend the log entry with some additional metadata:application
* `user_id`, `identity_id`, `actor_id`, `account_id` (order shows precedence) - the ID of the user that is performing the action.
* `user_id`, `identity_id`, `actor_id`, `account_id` (ordered by precedence) - the ID of the user that is performing the action.
It will be included along with the error report for Google Cloud Error Reporting;
For list of other well-known metadata keys see "Metadata" in `LoggerJSON`.
Expand Down Expand Up @@ -143,10 +143,12 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
defp log_level(:debug), do: "DEBUG"
defp log_level(_), do: "DEFAULT"

@doc false
def format_binary_message(binary) do
%{message: IO.chardata_to_string(binary)}
end

@doc false
def format_structured_message(map) when is_map(map) do
map
end
Expand All @@ -155,6 +157,7 @@ defmodule LoggerJSON.Formatters.GoogleCloud do
Enum.into(keyword, %{})
end

@doc false
# https://cloud.google.com/error-reporting/docs/formatting-error-messages
def format_crash_reason(binary, {{:EXIT, pid}, reason}, service_context, meta) do
stacktrace = Exception.format_banner({:EXIT, pid}, reason, [])
Expand Down
6 changes: 1 addition & 5 deletions lib/logger_json/plug.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
defmodule LoggerJSON.Plug do
@moduledoc """
A Plug to log request information in JSON format.
It works by setting up a `Plug.Conn.register_before_send/2` callback
which logs a structured message with `conn` and `latency` keys,
that are handled by the formatter when the message is encoded.
A telemetry handler that logs request information in JSON format.
This module is not recommended to be used in production, as it can be
costly to log every single database query.
Expand Down
5 changes: 3 additions & 2 deletions lib/logger_json/redactor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ defmodule LoggerJSON.Redactor do
@moduledoc """
This module provides a behaviour which allows to redact sensitive information from logs.
Note: redactor will not be applied on `Jason.Fragment` structs.
*Note*: redactor will not be applied on `Jason.Fragment` structs. For more information
about encoding and redacting see `LoggerJSON.Formatter.RedactorEncoder.encode/2`.
"""

@doc """
Takes a key and a value and returns a redacted value.
This callback will be applied on key-value pairs, like elements of structs, maps or keyword lists.
"""
@callback redact(key :: term(), value :: term(), opts :: term()) :: term()
@callback redact(key :: String.t(), value :: term(), opts :: term()) :: term()
end
3 changes: 1 addition & 2 deletions lib/logger_json/redactors/redact_keys.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ defmodule LoggerJSON.Redactors.RedactKeys do
]}
```
Keep in mind that LoggerJSON will convert key type to string before comparing it
with the list of keys to redact.
Keep in mind that the key will be converted to binary before sending it to the redactor.
"""

@behaviour LoggerJSON.Redactor
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule LoggerJSON.Mixfile do
use Mix.Project

@source_url "https://github.com/Nebo15/logger_json"
@version "6.0.0-rc.0"
@version "6.0.0-rc.1"

def project do
[
Expand Down
2 changes: 1 addition & 1 deletion test/logger_json/formatter/redactor_encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule LoggerJSON.Formatter.RedactorEncoderTest do

defmodule PasswordStruct, do: defstruct(password: "foo")

@redactors [{LoggerJSON.Redactors.RedactKeys, [:password]}]
@redactors [{LoggerJSON.Redactors.RedactKeys, ["password"]}]

describe "encode/2" do
test "allows nils" do
Expand Down

0 comments on commit e31100d

Please sign in to comment.