Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace gen_event with logger_filter #138

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/shoehorn/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ defmodule Shoehorn.Application do

@impl Application
def start(_type, _args) do
env = Application.get_all_env(:shoehorn)
if using_shoehorn?() do
opts = Application.get_all_env(:shoehorn)
:error_logger.add_report_handler(Shoehorn.ReportHandler, opts)
Shoehorn.ReportHandler.init_handler()
end

opts = [strategy: :one_for_one, name: Shoehorn.Supervisor]
Supervisor.start_link([], opts)
Supervisor.start_link([{Shoehorn.ReportHandler, env}], opts)
end

defp using_shoehorn?() do
Expand Down
40 changes: 40 additions & 0 deletions lib/shoehorn/filter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Shoehorn.Filter do
@moduledoc false

@doc """
A filter for handling Application.start/1,2 and Application.stop/1 events. Always return :ignore so that other filters
can continue to handle the events as they please.
"""
def filter(_event = %{msg: msg}, _extra) do
maybe_log_message(msg)
:ignore
end

def filter(_event, _extra) do
:ignore
end

def maybe_log_message(
{:report,
%{
label: {:application_controller, :progress},
report: [application: app, started_at: _node]
}}
) do
GenServer.cast(Shoehorn.ReportHandler, {:started, app})
end

def maybe_log_message(
{:report,
%{
label: {:application_controller, :exit},
report: [application: app, exited: reason, type: _type]
}}
) do
GenServer.cast(Shoehorn.ReportHandler, {:exit, app, reason})
end

def maybe_log_message(_msg) do
:ok
end
end
61 changes: 21 additions & 40 deletions lib/shoehorn/report_handler.ex
Original file line number Diff line number Diff line change
@@ -1,58 +1,36 @@
defmodule Shoehorn.ReportHandler do
@moduledoc false

@behaviour :gen_event

alias Shoehorn.Handler
use GenServer

@shutdown_timer 30_000

@impl :gen_event
def init(opts) do
shutdown_timer = opts[:shutdown_timer] || @shutdown_timer

{:ok,
%{
handler: Handler.init(opts),
shutdown_timer: shutdown_timer
}}
def init_handler() do
current_filters = :logger.get_primary_config() |> find_filters()
shoehorn_filters = [
shoehorn_filter: {&Shoehorn.Filter.filter/2, []}
]
# put the shoehorn filter to the front of the list to make sure it handles the message first.
:logger.set_primary_config(:filters, shoehorn_filters ++ current_filters)
end

@impl :gen_event
def handle_call(_, s) do
{:ok, :ok, s}
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end

@impl :gen_event
def handle_event({:info_report, _pid, {_, :std_info, info}}, s) when is_list(info) do
case Keyword.get(info, :exited) do
nil ->
{:ok, s}

reason ->
app = Keyword.get(info, :application)
{:ok, exited(app, reason, s)}
end
end

def handle_event({:info_report, _pid, {_, :progress, info}}, s) when is_list(info) do
case Keyword.get(info, :started_at) do
nil ->
{:ok, s}

_node ->
app = Keyword.get(info, :application)
{:ok, started(app, s)}
end
def init(opts) do
shutdown_timer = opts[:shutdown_timer] || @shutdown_timer
state = %{handler: Handler.init(opts), shutdown_timer: shutdown_timer}
{:ok, state}
end

def handle_event(_event, s) do
{:ok, s}
def handle_cast({:exit, app, reason}, s) do
{:noreply, exited(app, reason, s)}
end

@impl :gen_event
def terminate(_args, _s) do
:ok
def handle_cast({:started, app}, s) do
{:noreply, started(app, s)}
end

defp exited(app, reason, s) do
Expand All @@ -75,4 +53,7 @@ defmodule Shoehorn.ReportHandler do

defp react({:halt, _}, _), do: :erlang.halt()
defp react({:continue, handler}, state), do: %{state | handler: handler}

defp find_filters(%{filters: filters}), do: filters
defp find_filters(_), do: []
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Shoehorn.MixProject do
[
app: :shoehorn,
version: @version,
elixir: "~> 1.10",
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
elixirc_paths: elixirc_paths(Mix.env()),
description: description(),
Expand Down