Erlang (OTP 21+) Logger Console Output Question (Garbled Prompt)

Stanislav Ledenev s.ledenev@REDACTED
Mon May 10 11:32:25 CEST 2021


>
> But logger messages are not directly sent to the group leader, therefore
> they are not captured by eunit and are logged in all cases.


"...not captured by eunit and are logged in all cases". It appears that
"all cases"
is the key here. Perhaps your tests are more complicated than mine cause
I've
never had any such problems.

I sketch out a simple example and everything works as expected:
-module(mylib).

-export([addition/2, subtraction/2]).

-include_lib("eunit/include/eunit.hrl").
-include_lib("kernel/include/logger.hrl").

addition(X, Y) ->
    ?LOG_NOTICE("Addition enter."),
    X + Y.

subtraction(X, Y) ->
    ?LOG_NOTICE("Subtraction enter."),
    X - Y.

addition_test() ->
    ?LOG_NOTICE("Testing addition."),
    ?assertEqual(addition(2, 3), 6).

subtraction_test() ->
    ?LOG_NOTICE("Testing subtraction."),
    ?assertEqual(subtraction(6, 5), 1).

Output:
G:\Projects\_Erlang\mylib> rebar3 eunit
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling mylib
===> Performing EUnit tests...
=NOTICE REPORT==== 10-May-2021::12:29:19.477000 ===
Testing addition.
=NOTICE REPORT==== 10-May-2021::12:29:19.499000 ===
Addition enter.
=NOTICE REPORT==== 10-May-2021::12:29:19.515000 ===
Testing subtraction.
=NOTICE REPORT==== 10-May-2021::12:29:19.515000 ===
Subtraction enter.
F.
Failures:

  1) mylib:addition_test/0
     Failure/Error: ?assertEqual(5, 6)
       expected: 5
            got: 6
     %% eunit_proc.erl:337:in `eunit_proc:with_timeout/3`
     Output:
     Output:

Finished in 0.187 seconds
2 tests, 1 failures
===> Error running tests





пн, 10 мая 2021 г. в 11:13, Nicolas Martyanoff <khaelin@REDACTED>:

> Stanislav Ledenev <s.ledenev@REDACTED> writes:
>
> >>
> >> If I'm not mistaken, this is also the reason eunit tests do not capture
> >> logger messages, making it hard to understand what was logged by a
> >> specific failing test.
> >
> > What do you mean by "eunit doesn't capture logger messages"?
> > Is it some specific case?
> > I have had some problems with capturing messages but it was my fault -
> > I misspointed the config file with logger_level set higher than the
> > category of my messages.
> > And another issue was with common_test.
> > You must enable verbose output to be able to see messages.
>
> Eunit introduces its own group leader for each test case, capturing
> printed messages, and displaying them in a small section associated with
> each failing test. This is really useful when trying to understand why a
> specific test failed.
>
> But logger messages are not directly sent to the group leader, therefore
> they are not captured by eunit and are logged in all cases. This is a
> problem because it is now really hard to understand what was logged
> (especially which errors) in each failing test.
>
> This is clearly a design issue: eunit was not taken into account when
> logger was introduced.
>
> I tried to work around it with custom log handlers in the past, but
> could never make it work, and I remember talking about it on #erlang,
> but the conclusion was that it was not fixable without patching eunit
> and/or logger.
>
> --
> Nicolas Martyanoff
> http://snowsyn.net
> khaelin@REDACTED
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210510/c6ee2bce/attachment-0001.htm>


More information about the erlang-questions mailing list