Skip to content

Commit

Permalink
Merge pull request #9 from xflagstudio/impl-gen-event-callback-func
Browse files Browse the repository at this point in the history
Fix gen_event callbacks
  • Loading branch information
kanmo authored Aug 7, 2018
2 parents 5ffed24 + bf2288f commit 0f8e2e5
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/logger_json_file_backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ defmodule LoggerJSONFileBackend do
@macro_env_fields Map.keys(%Macro.Env{})
@formatter if Version.compare(System.version(), "1.6.0") == :lt, do: Logger.Utils, else: Logger.Formatter

@impl :gen_event
def init({__MODULE__, name}) do
{:ok, configure(name, [])}
end

@impl :gen_event
def handle_call({:configure, opts}, %{name: name} = state) do
{:ok, :ok, configure(name, opts, state)}
end

@impl :gen_event
def handle_call(:path, %{path: path} = state) do
{:ok, {:ok, path}, state}
end

@impl :gen_event
def handle_event({level, _gl, {Logger, msg, ts, md}}, %{level: min_level}=state) do
if is_nil(min_level) or Logger.compare_levels(level, min_level) != :lt do
log_event(level, msg, ts, md, state)
Expand All @@ -24,10 +28,26 @@ defmodule LoggerJSONFileBackend do
end
end

@impl :gen_event
def handle_event(:flush, state) do
{:ok, state}
end

@impl :gen_event
def handle_info(_msg, state) do
{:ok, state}
end

@impl :gen_event
def terminate(_reason, _state) do
:ok
end

@impl :gen_event
def code_change(_old, state, _extra) do
{:ok, state}
end

defp log_event(level, msg, ts, md, %{path: path, io_device: nil}=state) when is_binary(path) do
case open_log(path) do
{:ok, io_device, inode} ->
Expand Down

0 comments on commit 0f8e2e5

Please sign in to comment.