Skip to content

Commit

Permalink
Add :bandit domain to logger metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrudel committed Jun 19, 2024
1 parent bc67149 commit b62a7eb
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 15 deletions.
7 changes: 5 additions & 2 deletions lib/bandit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,14 @@ defmodule Bandit do
|> ThousandIsland.start_link()
|> case do
{:ok, pid} ->
startup_log && Logger.log(startup_log, info(scheme, display_plug, pid))
startup_log && Logger.log(startup_log, info(scheme, display_plug, pid), domain: [:bandit])
{:ok, pid}

{:error, {:shutdown, {:failed_to_start_child, :listener, :eaddrinuse}}} = error ->
Logger.error([info(scheme, display_plug, nil), " failed, port #{port} already in use"])
Logger.error([info(scheme, display_plug, nil), " failed, port #{port} already in use"],
domain: [:bandit]
)

error

{:error, _} = error ->
Expand Down
2 changes: 1 addition & 1 deletion lib/bandit/clock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Bandit.Clock do
:ets.lookup_element(__MODULE__, :date_header, 2)
rescue
ArgumentError ->
Logger.warning("Header timestamp couldn't be fetched from ETS cache")
Logger.warning("Header timestamp couldn't be fetched from ETS cache", domain: [:bandit])
get_date_header()
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bandit/http2/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ defmodule Bandit.HTTP2.Connection do
# Catch-all handler for unknown frame types

def handle_frame(%Bandit.HTTP2.Frame.Unknown{} = frame, _socket, connection) do
Logger.warning("Unknown frame (#{inspect(Map.from_struct(frame))})")
Logger.warning("Unknown frame (#{inspect(Map.from_struct(frame))})", domain: [:bandit])
connection
end

Expand Down
13 changes: 10 additions & 3 deletions lib/bandit/http2/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ defmodule Bandit.HTTP2.Handler do
rescue
error ->
case Keyword.get(state.opts.http, :log_protocol_errors, :short) do
:short -> Logger.error(Exception.format_banner(:error, error, __STACKTRACE__))
:verbose -> Logger.error(Exception.format(:error, error, __STACKTRACE__))
false -> :ok
:short ->
Logger.error(Exception.format_banner(:error, error, __STACKTRACE__),
domain: [:bandit]
)

:verbose ->
Logger.error(Exception.format(:error, error, __STACKTRACE__), domain: [:bandit])

false ->
:ok
end

{:close, state}
Expand Down
2 changes: 1 addition & 1 deletion lib/bandit/http2/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ defmodule Bandit.HTTP2.Stream do
case do_recv(stream, timeout) do
{:headers, trailers, stream} ->
no_pseudo_headers!(trailers)
Logger.warning("Ignoring trailers #{inspect(trailers)}")
Logger.warning("Ignoring trailers #{inspect(trailers)}", domain: [:bandit])
do_read_data(stream, max_bytes, timeout, acc)

{:data, data, stream} ->
Expand Down
5 changes: 4 additions & 1 deletion lib/bandit/initial_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ defmodule Bandit.InitialHandler do
def handle_connection(socket, state) do
case {state.http_1_enabled, state.http_2_enabled, alpn_protocol(socket), sniff_wire(socket)} do
{_, _, _, :likely_tls} ->
Logger.warning("Connection that looks like TLS received on a clear channel")
Logger.warning("Connection that looks like TLS received on a clear channel",
domain: [:bandit]
)

{:close, state}

{_, true, Bandit.HTTP2.Handler, Bandit.HTTP2.Handler} ->
Expand Down
6 changes: 4 additions & 2 deletions lib/bandit/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ defmodule Bandit.Logger do
) :: :ok
def log_error(event, measurements, metadata, _config) do
Logger.error(
"#{inspect(event)} metadata: #{inspect(metadata)}, measurements: #{inspect(measurements)}"
"#{inspect(event)} metadata: #{inspect(metadata)}, measurements: #{inspect(measurements)}",
domain: [:bandit]
)
end

Expand All @@ -82,7 +83,8 @@ defmodule Bandit.Logger do
) :: :ok
def log_info(event, measurements, metadata, _config) do
Logger.info(
"#{inspect(event)} metadata: #{inspect(metadata)}, measurements: #{inspect(measurements)}"
"#{inspect(event)} metadata: #{inspect(metadata)}, measurements: #{inspect(measurements)}",
domain: [:bandit]
)
end
end
13 changes: 9 additions & 4 deletions lib/bandit/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,14 @@ defmodule Bandit.Pipeline do
Bandit.Telemetry.stop_span(span, %{}, %{error: error.message})

case Keyword.get(opts.http, :log_protocol_errors, :short) do
:short -> Logger.error(Exception.format_banner(:error, error, stacktrace))
:verbose -> Logger.error(Exception.format(:error, error, stacktrace))
false -> :ok
:short ->
Logger.error(Exception.format_banner(:error, error, stacktrace), domain: [:bandit])

:verbose ->
Logger.error(Exception.format(:error, error, stacktrace), domain: [:bandit])

false ->
:ok
end

# We want to do this at the end of the function, since the HTTP2 stack may kill this process
Expand All @@ -225,7 +230,7 @@ defmodule Bandit.Pipeline do
status = error |> Plug.Exception.status() |> Plug.Conn.Status.code()

if status in Keyword.get(opts.http, :log_exceptions_with_status_codes, 500..599) do
Logger.error(Exception.format(:error, error, stacktrace))
Logger.error(Exception.format(:error, error, stacktrace), domain: [:bandit])
Bandit.HTTPTransport.send_on_error(transport, error)
{:error, error}
else
Expand Down

0 comments on commit b62a7eb

Please sign in to comment.