Skip to content

Commit

Permalink
Prepare for upcoming Elixir v1.9.0
Browse files Browse the repository at this point in the history
API still can be changed.
  • Loading branch information
liveforeverx committed Mar 19, 2019
1 parent a8c9695 commit 658c4fe
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
84 changes: 62 additions & 22 deletions lib/lager_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,60 @@ defmodule LagerLogger do
end
end

@doc false
def handle_event({:log, lager_msg}, mask) do
%{mode: mode, truncate: truncate, level: min_level, utc_log: utc_log?} = Logger.Config.__data__
level = severity_to_level(:lager_msg.severity(lager_msg))
# Elixir v1.9.0 has refactored logging, so now data fetched directly for log level
if function_exported?(Logger.Config, :translation_data, 0) do
@doc false
def handle_event({:log, lager_msg}, mask) do
level = severity_to_level(:lager_msg.severity(lager_msg))
is_loggable? = :lager_util.is_loggable(lager_msg, mask, __MODULE__)

case is_loggable? and Logger.Config.log_data(level) do
false ->
{:ok, mask}

{:discard, _} ->
{:ok, mask}

if :lager_util.is_loggable(lager_msg, mask, __MODULE__) and
Logger.compare_levels(level, min_level) != :lt do
{mode, %{truncate: truncate, level: min_level, utc_log: utc_log?}} ->
metadata = :lager_msg.metadata(lager_msg) |> normalize_pid

metadata = :lager_msg.metadata(lager_msg) |> normalize_pid
# lager_msg's message is already formatted chardata
message = Logger.Utils.truncate(:lager_msg.message(lager_msg), truncate)

# lager_msg's message is already formatted chardata
message = Logger.Utils.truncate(:lager_msg.message(lager_msg), truncate)
# Lager always uses local time and converts it when formatting using :lager_util.maybe_utc
timestamp = timestamp(:lager_msg.timestamp(lager_msg), utc_log?)

# Lager always uses local time and converts it when formatting using :lager_util.maybe_utc
timestamp = timestamp(:lager_msg.timestamp(lager_msg), utc_log?)
group_leader = find_group_leader(metadata)

group_leader = case Keyword.fetch(metadata, :pid) do
{:ok, pid} when is_pid(pid) ->
case Process.info(pid, :group_leader) do
{:group_leader, gl} -> gl
nil -> Process.group_leader # if pid dead, pretend it's us as must be a pid
end
_ -> Process.group_leader # if lager didn't give us a pid just pretend it's us
_ = notify(mode, {level, group_leader, {Logger, message, timestamp, metadata}})
{:ok, mask}
end
end
else
@doc false
def handle_event({:log, lager_msg}, mask) do
%{mode: mode, truncate: truncate, level: min_level, utc_log: utc_log?} =
Logger.Config.__data__()

_ = notify(mode, {level, group_leader, {Logger, message, timestamp, metadata}})
{:ok, mask}
else
{:ok, mask}
level = severity_to_level(:lager_msg.severity(lager_msg))

if :lager_util.is_loggable(lager_msg, mask, __MODULE__) and
Logger.compare_levels(level, min_level) != :lt do
metadata = :lager_msg.metadata(lager_msg) |> normalize_pid

# lager_msg's message is already formatted chardata
message = Logger.Utils.truncate(:lager_msg.message(lager_msg), truncate)

# Lager always uses local time and converts it when formatting using :lager_util.maybe_utc
timestamp = timestamp(:lager_msg.timestamp(lager_msg), utc_log?)

group_leader = find_group_leader(metadata)

_ = notify(mode, {level, group_leader, {Logger, message, timestamp, metadata}})
{:ok, mask}
else
{:ok, mask}
end
end
end

Expand Down Expand Up @@ -162,4 +187,19 @@ defmodule LagerLogger do
defp severity_to_level(:critical), do: :error
defp severity_to_level(:alert), do: :error
defp severity_to_level(:emergency), do: :error

defp find_group_leader(metadata) do
case Keyword.fetch(metadata, :pid) do
{:ok, pid} when is_pid(pid) ->
case Process.info(pid, :group_leader) do
{:group_leader, gl} -> gl
# if pid dead, pretend it's us as must be a pid
nil -> Process.group_leader()
end

# if lager didn't give us a pid just pretend it's us
_ ->
Process.group_leader()
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule LagerLogger.Mixfile do
def project do
[app: :lager_logger,
version: "1.0.5",
elixir: ">= 1.1.0 and < 1.7.0",
elixir: ">= 1.1.0 and < 1.9.0",
package: package(),
description: description(),
deps: deps()]
Expand Down

0 comments on commit 658c4fe

Please sign in to comment.