Skip to content

Use Erlang logger handlers in ExUnit.CaptureLog #12408

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 8 commits into from
Feb 17, 2023
Merged

Conversation

whatyouhide
Copy link
Member

Part of #9465.

@albertored
Copy link
Contributor

I'm experiencing a strange behavior related to this, logs from erlang libraries in my elixir project are not captured by ExUnit and show up in the console while running tests. Is that known and is this the problem this PR is solving?

(@whatyouhide sorry for the noise but I just found this problem today and this PR "è arrivata a fagiuolo" 😄 )

status =
with {:ok, config} <- :logger.get_handler_config(:default),
:ok <- :logger.remove_handler(:default) do
:ok <- :logger.remove_handler(:default),
:ok <- :logger.add_handler(@name, __MODULE__, handler_cfg) do
Copy link
Member

Choose a reason for hiding this comment

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

We should attach this handler even if there is no default. So I recommend moving it out of the with and keeping these two code paths separated.

Copy link
Member

Choose a reason for hiding this comment

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

We should also remember to remove this handler on maybe_add_console.

Copy link
Member

Choose a reason for hiding this comment

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

maybe_add_console should probably be renamed to revert_handlers or something :D

@spec with_log(keyword, (-> any)) :: {any, String.t()}
def with_log(opts \\ [], fun) do
@spec with_log(keyword, (-> result)) :: {result, String.t()} when result: any
def with_log(opts \\ [], fun) when is_list(opts) do
opts = Keyword.put_new(opts, :level, nil)
{:ok, string_io} = StringIO.open("")

try do
:ok = add_capture(string_io, opts)
Copy link
Member

Choose a reason for hiding this comment

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

We should remove this capture. We are likely capturing everything twice right now. :) plus, we need to pass the level to the ets table and only write if it matches the ETS table level.

Copy link
Member

Choose a reason for hiding this comment

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

Note we can default the level to :all.

@whatyouhide
Copy link
Member Author

@josevalim we had the same ideas, yep. I just implemented all of that, ready for another review!

@@ -59,11 +66,17 @@ defmodule ExUnit.CaptureServer do
{:reply, :ok, release_device(ref, config)}
end

def handle_call({:log_capture_on, pid}, _from, config) do
def handle_call({:log_capture_on, pid, string_io, opts}, _from, config) do
ref = Process.monitor(pid)
refs = Map.put(config.log_captures, ref, true)
Copy link
Member Author

Choose a reason for hiding this comment

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

I think we'll be able to get rid of this and only use the ETS table to store these, but I'd rather do one thing at a time and do that in another PR. 🙃

@whatyouhide whatyouhide mentioned this pull request Feb 16, 2023
14 tasks
Copy link
Member

@josevalim josevalim left a comment

Choose a reason for hiding this comment

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

One tiny comment and ship it!

Comment on lines +73 to +74
{level, opts} = Keyword.pop(opts, :level)
true = :ets.insert(@ets, {ref, string_io, level || :all})
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
{level, opts} = Keyword.pop(opts, :level)
true = :ets.insert(@ets, {ref, string_io, level || :all})
{level, opts} = Keyword.pop(opts, :level, :all)
true = :ets.insert(@ets, {ref, string_io, level})

@whatyouhide whatyouhide merged commit 10e14ea into main Feb 17, 2023
@whatyouhide whatyouhide deleted the al/new-capture-log branch February 17, 2023 06:54
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.

3 participants