-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logger improvements (foundations) (#1556)
* Move the `ImAlive` plug to be before the `Telemetry` plug * Don't log `mfa` (by default) and `pid` * Use `x-forwarded-for`, if it exists * Hook into Phoenix telemetry for logging This is the recommended approach and means we don't need custom plugs.
- Loading branch information
Showing
8 changed files
with
91 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
defmodule NervesHub.Logger do | ||
require Logger | ||
|
||
@doc false | ||
def install() do | ||
handlers = %{ | ||
[:phoenix, :endpoint, :stop] => &__MODULE__.phoenix_endpoint_stop/4 | ||
} | ||
|
||
for {key, fun} <- handlers do | ||
:ok = :telemetry.attach({__MODULE__, key}, key, fun, :ok) | ||
end | ||
|
||
:ok | ||
end | ||
|
||
# Phoenix request logging | ||
|
||
@doc false | ||
def phoenix_endpoint_stop(_, %{duration: duration}, %{conn: conn} = metadata, _) do | ||
case log_level(metadata[:options][:log], conn) do | ||
false -> | ||
:ok | ||
|
||
level -> | ||
Logger.log(level, fn -> | ||
Logfmt.encode( | ||
duration: duration(duration), | ||
method: conn.method, | ||
path: request_path(conn), | ||
status: conn.status, | ||
remote_ip: formatted_ip(conn) | ||
) | ||
end) | ||
end | ||
end | ||
|
||
# Helper functions | ||
|
||
defp duration(duration) do | ||
duration = System.convert_time_unit(duration, :native, :microsecond) | ||
|
||
if duration > 1000 do | ||
[duration |> div(1000) |> Integer.to_string(), "ms"] | ||
else | ||
[Integer.to_string(duration), "µs"] | ||
end | ||
|> Enum.join() | ||
end | ||
|
||
defp request_path(%{request_path: request_path, query_string: query_string}) | ||
when query_string not in ["", nil], | ||
do: request_path <> "?" <> query_string | ||
|
||
defp request_path(%{request_path: request_path}), do: request_path | ||
defp request_path(_), do: nil | ||
|
||
defp formatted_ip(conn) do | ||
case Plug.Conn.get_req_header(conn, "x-forwarded-for") do | ||
[ips] -> | ||
ips | ||
|> String.split(",") | ||
|> List.first() | ||
|> String.trim() | ||
|
||
_ -> | ||
conn.remote_ip | ||
|> :inet_parse.ntoa() | ||
|> to_string() | ||
end | ||
end | ||
|
||
defp log_level(nil, _conn), do: :info | ||
defp log_level(level, _conn) when is_atom(level), do: level | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
defmodule NervesHub.LoggerFormatter do | ||
@metadata_ignore_list [:line, :file, :domain, :application, :pid, :mfa] | ||
@pattern Logger.Formatter.compile("$time [$level] $metadata$message\n") | ||
|
||
def format(level, message, timestamp, metadata) do | ||
metadata = Keyword.drop(metadata, [:line, :file, :domain, :application]) | ||
metadata = Keyword.drop(metadata, ignore_list()) | ||
Logger.Formatter.format(@pattern, level, message, timestamp, metadata) | ||
end | ||
|
||
defp ignore_list() do | ||
if Application.get_env(:nerves_hub, :log_include_mfa) do | ||
@metadata_ignore_list -- [:mfa] | ||
else | ||
@metadata_ignore_list | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.