-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logger backend which sends data to UI
- Loading branch information
1 parent
efab4f9
commit 52e8308
Showing
2 changed files
with
60 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
defmodule Crawly.Loggers.SendToUiBackend do | ||
# TODO: Write doc | ||
# Initialize the configuration | ||
def init({__MODULE__, name}) do | ||
{:ok, configure(name, [])} | ||
end | ||
|
||
def handle_call({:configure, opts}, %{name: name} = state) do | ||
{:ok, :ok, configure(name, opts, state)} | ||
end | ||
|
||
# Handle the flush event | ||
def handle_event(:flush, state) do | ||
{:ok, state} | ||
end | ||
|
||
# Handle any log messages that are sent across | ||
def handle_event( | ||
{_level, _group_leader, {Logger, message, _timestamp, metadata}}, | ||
%{destination: {node, module, function}} = state | ||
) do | ||
case Keyword.get(metadata, :crawl_id, nil) do | ||
nil -> | ||
:ignore | ||
|
||
crawl_id -> | ||
:rpc.cast(node, module, function, [crawl_id, message]) | ||
end | ||
|
||
{:ok, state} | ||
end | ||
|
||
defp configure(name, []) do | ||
case Application.get_env(:logger, name, []) do | ||
[] -> | ||
raise "Destination was not configured" | ||
|
||
config -> | ||
%{name: name, destination: Keyword.get(config, :destination)} | ||
end | ||
end | ||
|
||
# We don't support any re-configuration so far | ||
defp configure(_name, _opts, state), do: state | ||
end |