Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from phoenixframework/cm-web-logger
Add web console logger and open file from client support
- Loading branch information
Showing
9 changed files
with
388 additions
and
52 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
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,35 @@ | ||
defmodule Phoenix.LiveReloader.WebConsoleLogger do | ||
@moduledoc false | ||
|
||
@registry Phoenix.LiveReloader.WebConsoleLoggerRegistry | ||
|
||
def attach_logger do | ||
if function_exported?(Logger, :default_formatter, 0) do | ||
:ok = | ||
:logger.add_handler(__MODULE__, __MODULE__, %{ | ||
formatter: Logger.default_formatter(colors: [enabled: false]) | ||
}) | ||
end | ||
end | ||
|
||
def child_spec(_args) do | ||
Registry.child_spec(name: @registry, keys: :duplicate) | ||
end | ||
|
||
def subscribe(prefix) do | ||
{:ok, _} = Registry.register(@registry, :all, prefix) | ||
:ok | ||
end | ||
|
||
# Erlang/OTP log handler | ||
def log(%{meta: meta, level: level} = event, config) do | ||
%{formatter: {formatter_mod, formatter_config}} = config | ||
iodata = formatter_mod.format(event, formatter_config) | ||
msg = IO.iodata_to_binary(iodata) | ||
|
||
Registry.dispatch(@registry, :all, fn entries -> | ||
for {pid, prefix} <- entries, | ||
do: send(pid, {prefix, %{level: level, msg: msg, meta: meta}}) | ||
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
Oops, something went wrong.