Skip to content

Emit notice logs in :console backend #11304

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

Merged
merged 17 commits into from
Oct 9, 2021

Conversation

riccardomanfrin
Copy link
Contributor

yes.. this is not very "backward compatible"..
I guess adding some config flag to enable it, and
default it to false.. would be proper (?)

@josevalim
Copy link
Member

Yes, unfortunately we cannot introduce this as it would break all existing Logger backends, as they will now receive a format they don't know how to handle. However, what we can do instead is to change the erlang_metadata_to_elixir_metadata function to have a erlang_level key in it. And we can use this erlang_level key in the console backend. Can you please try this route?

@zoten
Copy link

zoten commented Oct 8, 2021

@josevalim an erl_level metadata is already injected into the metadata, I think we could use directly this.

Is a little overhead (keyword find) in re-translating back elixir level into erlang level acceptable in :console backend?

yes.. this is not very "backward compatible"..
I guess adding some config flag to enable it, and
default it to false.. would be proper (?)
@riccardomanfrin
Copy link
Contributor Author

I've added support for :notice in the formatter.ex and changed the console.ex backend.. I hope this is limiting the propagation of the :notice to only the console backend, leaving the rest of backends happy (the key was there already as erl_level)

Is this what you meant?

It's in any case breaking the format of preexisting log collections in case someone was logging to notice.

Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: Alberto Sartori <alberto.sartori.as@gmail.com>
@albertored
Copy link
Contributor

we should also convert back erlang :warning level to :warn

@albertored
Copy link
Contributor

In addition, the same logic should be used also in the buffer_event function so maybe is cleaner to move it in the handle_event function.

In this way, it goes also through the meet_level? function so it is possible to specify erlang log levels as the minimum level for a message to be logged. Not sure if this is the intended behavior though

@riccardomanfrin
Copy link
Contributor Author

In addition, the same logic should be used also in the buffer_event function so maybe is cleaner to move it in the handle_event function.

In this way, it goes also through the meet_level? function so it is possible to specify erlang log levels as the minimum level for a message to be logged. Not sure if this is the intended behavior though

unintended at all.

level = case level do
:warning -> :warn
_ -> level
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, let's not convert back. Logger.warn is already deprecated, so it is a good opportunity to move to warning. :)

@josevalim
Copy link
Member

@riccardomanfrin also please take a look at CI. We need to add at least two tests:

  1. That we are now logging notice in the actual message
  2. That we can filter on the backend using notice and similar

What about pad levels?
@josevalim
Copy link
Member

@riccardomanfrin there is a failure in ex_unit suite now because of the changes: make test_ex_unit. :) Should be a quick fix!

@josevalim
Copy link
Member

/cc @hauleth in case you want to review it too.

Copy link
Contributor

@hauleth hauleth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring all the comments, I have different concept how we can add support for more features from Erlang logger instead.

  • add new optional callback for the backends, for example type/0 that will return :legacy | :rich and will default to :legacy if not defined
  • for :legacy backends receives message in the current, tuple based, format
  • for :rich (can use different name there) backend receives message in "raw" format from the Erlang (possibly with some alterations to wrap :report_cb for example)

This approach should give us a way to make full transition in future (possibly still in multiple steps).

What do you think @josevalim about such approach? It would give a way to have smother transition period, and possibly migrate to "new" format even without bumping Logger's major version (which would imply bumping Elixir's major version).

@@ -138,6 +138,8 @@ defmodule Logger.Backends.Console do
def handle_event({level, _gl, {Logger, msg, ts, md}}, state) do
%{level: log_level, ref: ref, buffer_size: buffer_size, max_buffer: max_buffer} = state

{:erl_level, level} = List.keyfind(md, :erl_level, 0, {:erl_level, level})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

md is defined to be keyword list, so why use List.keyfind/4 instead of Keyword.get/3?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are fine. List.keyfind can be a tiny bit more efficient.

@@ -163,10 +163,10 @@ defmodule Logger.Formatter do
defp output(:levelpad, level, _, _, _), do: levelpad(level)
defp output(other, _, _, _, _), do: other

defp levelpad(:debug), do: ""
# TODO: Deprecate me on Elixir v1.13+ or later
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

@josevalim josevalim Oct 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

levelpad is not very useful now when one of the levels is "critical". There will be just too much padding on something like "info" (4 chars to be more precise).

Comment on lines 237 to 239
alert: Keyword.get(colors, :debug, :red),
critical: Keyword.get(colors, :debug, :red),
emergency: Keyword.get(colors, :debug, :red),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be:

Suggested change
alert: Keyword.get(colors, :debug, :red),
critical: Keyword.get(colors, :debug, :red),
emergency: Keyword.get(colors, :debug, :red),
alert: Keyword.get(colors, :alert, :red),
critical: Keyword.get(colors, :critical, :red),
emergency: Keyword.get(colors, :emergency, :red),

However in my logger_colorful I have used background color for all these levels to mark that it is "higher than error".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Those should be all error. We should add a fallback for custom colors in another PR.

error: Keyword.get(colors, :error, :red),
notice: Keyword.get(colors, :info, :normal),
enabled: Keyword.get(colors, :enabled, IO.ANSI.enabled?())
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Order of the keys in this map is now weird. Earlier it was in decreasing verbosity, and now it is "whatever".

@josevalim
Copy link
Member

josevalim commented Oct 9, 2021

@hauleth thanks for the review. What you described is precisely the plan for #7328. The idea is that the old handlers will be wrapped by an internal gen_event handler and the new handle_event message will be much closer to what Erlang emits. I would say this PR is something necessary to help us get there, so the backend is already ready for the new version.

@josevalim josevalim merged commit 2af0411 into elixir-lang:master Oct 9, 2021
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@hauleth
Copy link
Contributor

hauleth commented Oct 9, 2021

@josevalim you probably meant #7928. I can see if I can craft something like that as a part of Hacktoberfest.

I wouldn't say that it is strictly needed to get us there, but for sure can ease transition period (unless I really will be able to craft my PR before 1.13).

@josevalim
Copy link
Member

Yes, correct on both. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants