Skip to content

Commit

Permalink
Add body to the plug-logged messages
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDryga committed May 14, 2024
1 parent fdb13e5 commit da02a37
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/logger_json/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule LoggerJSON.Ecto do
| false
) :: :ok | {:error, :already_exists}
def attach(name, event, level) do
:telemetry.attach(name, event, &telemetry_logging_handler/4, level)
:telemetry.attach(name, event, &__MODULE__.telemetry_logging_handler/4, level)
end

@doc """
Expand Down
44 changes: 42 additions & 2 deletions lib/logger_json/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule LoggerJSON.Plug do
LoggerJSON.Plug.attach("logger-json-phoenix-requests", [:phoenix, :endpoint, :stop], :info)
"""
def attach(name, event, level) do
:telemetry.attach(name, event, &telemetry_logging_handler/4, level)
:telemetry.attach(name, event, &__MODULE__.telemetry_logging_handler/4, level)
end

@doc """
Expand All @@ -58,7 +58,47 @@ defmodule LoggerJSON.Plug do
duration = System.convert_time_unit(duration, :native, :microsecond)

if level = level(level, conn) do
Logger.log(level, "", conn: conn, duration_μs: duration)
Logger.log(
level,
fn ->
%{
method: method,
request_path: request_path,
state: state,
status: status
} = conn

[
method,
?\s,
request_path,
?\s,
"[",
connection_type(state),
?\s,
status(status),
"in ",
duration(duration),
"]"
]
end,
conn: conn,
duration_μs: duration
)
end
end

defp connection_type(:set_chunked), do: "Chunked"
defp connection_type(_), do: "Sent"

defp status(nil), do: ""
defp status(status), do: [status |> Plug.Conn.Status.code() |> Integer.to_string(), ?\s]

def duration(duration) do
if duration > 1000 do
[duration |> div(1000) |> Integer.to_string(), "ms"]
else
[Integer.to_string(duration), "µs"]
end
end

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.1"
@version "6.0.0-rc.2"

def project do
[
Expand Down
10 changes: 5 additions & 5 deletions test/logger_json/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ defmodule LoggerJSON.PlugTest do

describe "telemetry_logging_handler/4" do
test "logs request latency and metadata" do
conn = Plug.Test.conn(:get, "/")
conn = Plug.Test.conn(:get, "/") |> Plug.Conn.put_status(200)

log =
capture_log(fn ->
telemetry_logging_handler(
[:phoenix, :endpoint, :stop],
%{duration: 5000},
%{duration: 500_000},
%{conn: conn},
:info
)
Expand All @@ -25,11 +25,11 @@ defmodule LoggerJSON.PlugTest do
end)

assert %{
"message" => "",
"metadata" => %{"duration_μs" => 5},
"message" => "GET / [Sent 200 in 500µs]",
"metadata" => %{"duration_μs" => 500},
"request" => %{
"client" => %{"ip" => "127.0.0.1", "user_agent" => nil},
"connection" => %{"method" => "GET", "path" => "/", "protocol" => "HTTP/1.1", "status" => nil}
"connection" => %{"method" => "GET", "path" => "/", "protocol" => "HTTP/1.1", "status" => 200}
}
} = decode_or_print_error(log)
end
Expand Down

0 comments on commit da02a37

Please sign in to comment.