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

Use a descentralized mode computation for Logger #8567

Merged
merged 12 commits into from Jan 5, 2019
@@ -133,15 +133,16 @@ defmodule Logger do
`:sync_threshold` messages in its queue, `Logger` will change
to *sync mode*, to apply backpressure to the clients.
`Logger` will return to *async mode* once the number of messages
in the queue is reduced to `sync_threshold * 0.75` messages.
Defaults to 20 messages. `:sync_threshold` can be set to `0` to force *sync mode*.
in the queue is reduced to one below the `sync_threshold`.
Defaults to 20 messages. `:sync_threshold` can be set to `0` to
force *sync mode*.
* `:discard_threshold` - if the `Logger` manager has more than
`:discard_threshold` messages in its queue, `Logger` will change
to *discard mode* and messages will be discarded directly in the
clients. `Logger` will return to *sync mode* once the number of
messages in the queue is reduced to `discard_threshold * 0.75`
messages. Defaults to 500 messages.
messages in the queue is reduced to one below `discard_threshold`.
This conversation was marked as resolved by josevalim

This comment has been minimized.

Copy link
@audiolion

audiolion Dec 26, 2018

The new comment above on https://github.com/elixir-lang/elixir/pull/8567/files#diff-e595abf4d143c9f920f1d59b61df7c35R136 uses one below the so suggesting adding the here to match

Suggested change
messages in the queue is reduced to one below `discard_threshold`.
messages in the queue is reduced to one below the `discard_threshold`.
Defaults to 500 messages.
* `:translator_inspect_opts` - when translating OTP reports and
errors, the last message and state must be inspected in the
@@ -501,10 +502,7 @@ defmodule Logger do
The `Logger` level can be changed via `configure/1`.
"""
@spec level() :: level
def level() do
%{level: level} = Logger.Config.__data__()
level
end
defdelegate level(), to: Logger.Config

@doc """
Compares log levels.
@@ -525,18 +523,7 @@ defmodule Logger do
"""
@spec compare_levels(level, level) :: :lt | :eq | :gt
def compare_levels(level, level) do
:eq
end

def compare_levels(left, right) do
if level_to_number(left) > level_to_number(right), do: :gt, else: :lt
end

defp level_to_number(:debug), do: 0
defp level_to_number(:info), do: 1
defp level_to_number(:warn), do: 2
defp level_to_number(:error), do: 3
defdelegate compare_levels(left, right), to: Logger.Config

@doc """
Configures the logger.
@@ -669,12 +656,9 @@ defmodule Logger do
def __should_log__(level) when level in @levels do
case __metadata__() do
{true, pdict} ->
%{mode: mode, level: min_level} = config = Logger.Config.__data__()

if compare_levels(level, min_level) != :lt and mode != :discard do
{level, config, pdict}
else
:error
case Logger.Config.log_data(level) do
{:discard, _config} -> :error
{mode, config} -> {level, mode, config, pdict}
end

{false, _} ->
@@ -683,8 +667,9 @@ defmodule Logger do
end

@doc false
def __do_log__({level, config, pdict}, chardata_or_fun, metadata) when is_list(metadata) do
%{utc_log: utc_log?, truncate: truncate, mode: mode} = config
def __do_log__({level, mode, config, pdict}, chardata_or_fun, metadata)
when is_list(metadata) do
%{utc_log: utc_log?, truncate: truncate} = config
metadata = [pid: self()] ++ into_metadata(metadata, pdict)

case normalize_message(chardata_or_fun, metadata) do
@@ -829,7 +814,7 @@ defmodule Logger do
Enum.any?(matching, fn filter ->
Enum.all?(filter, fn
{:level_lower_than, min_level} ->
compare_levels(level, min_level) == :lt
Logger.Config.compare_levels(level, min_level) == :lt

{k, v} when is_atom(k) ->
Keyword.fetch(compile_metadata, k) == {:ok, v}
@@ -845,7 +830,7 @@ defmodule Logger do
defp maybe_log(level, data, metadata, caller) do
min_level = Application.get_env(:logger, :compile_time_purge_level, :debug)

if compare_levels(level, min_level) != :lt do
if Logger.Config.compare_levels(level, min_level) != :lt do
macro_log(level, data, metadata, caller)
else
no_log(data, metadata)
@@ -17,19 +17,19 @@ defmodule Logger.App do
[%{id: Logger.ErrorHandler, start: {Logger.Watcher, :start_link, [arg]}}]
end

config = Logger.Config.new()

children = [
%{
id: :gen_event,
start: {:gen_event, :start_link, [{:local, Logger}]},
modules: :dynamic
},
{Logger.Watcher, {Logger, Logger.Config, []}},
{Logger.Watcher, {Logger, Logger.Config, config}},
Logger.BackendSupervisor
| otp_children
]

config = Logger.Config.new()

case Supervisor.start_link(children, strategy: :rest_for_one, name: Logger.Supervisor) do
{:ok, sup} ->
if otp_logger?() do
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.