Ver a proveniência

Support variables as arguments to lager:info and friends

As long as *one* of the arguments is a literal, lager can figure out
what you're trying to do and (re)arrange the arguments as necessary.
pull/54/head
Andrew Thompson há 12 anos
ascendente
cometimento
95fdf935b9
2 ficheiros alterados com 46 adições e 0 eliminações
  1. +16
    -0
      src/lager_transform.erl
  2. +30
    -0
      test/lager_test_backend.erl

+ 16
- 0
src/lager_transform.erl Ver ficheiro

@ -111,6 +111,18 @@ transform_statement({call, Line, {remote, _Line1, {atom, _Line2, lager},
{cons, _, {tuple, _, _}, _} ->
{concat_lists(Arg1, DefaultAttrs),
Arg2, {atom, Line, none}};
{var, _, _} ->
%% crap, its a variable. look at the second
%% argument to see if it is a string
case Arg2 of
{string, _, _} ->
{concat_lists(Arg1, DefaultAttrs),
Arg2, {atom, Line, none}};
_ ->
%% not a string, going to have to guess
%% its the argument list
{DefaultAttrs, Arg1, Arg2}
end;
_ ->
{DefaultAttrs, Arg1, Arg2}
end;
@ -146,6 +158,10 @@ transform_statement(Stmt) ->
Stmt.
%% concat 2 list ASTs by replacing the terminating [] in A with the contents of B
concat_lists({var, Line, Name}, B) ->
%% concatenating a var with a cons
{call, Line, {remote, Line, {atom, Line, lists},{atom, Line, flatten}},
[{cons, Line, {var, Line, Name}, B}]};
concat_lists({nil, _Line}, B) ->
B;
concat_lists({cons, Line, Element, Tail}, B) ->

+ 30
- 0
test/lager_test_backend.erl Ver ficheiro

@ -180,6 +180,36 @@ lager_test_() ->
ok
end
},
{"variables inplace of literals in logging statements work",
fun() ->
?assertEqual(0, count()),
Attr = [{a, alpha}, {b, beta}],
Fmt = "format ~p",
Args = [world],
lager:info(Attr, "hello"),
lager:info(Attr, "hello ~p", [world]),
lager:info(Fmt, [world]),
lager:info("hello ~p", Args),
lager:info(Attr, "hello ~p", Args),
lager:info([{d, delta}, {g, gamma}], Fmt, Args),
?assertEqual(6, count()),
{_Level, _Time, Message, Metadata} = pop(),
?assertMatch([{a, alpha}, {b, beta}|_], Metadata),
?assertEqual("hello", lists:flatten(Message)),
{_Level, _Time2, Message2, _Metadata2} = pop(),
?assertEqual("hello world", lists:flatten(Message2)),
{_Level, _Time3, Message3, _Metadata3} = pop(),
?assertEqual("format world", lists:flatten(Message3)),
{_Level, _Time4, Message4, _Metadata4} = pop(),
?assertEqual("hello world", lists:flatten(Message4)),
{_Level, _Time5, Message5, _Metadata5} = pop(),
?assertEqual("hello world", lists:flatten(Message5)),
{_Level, _Time6, Message6, Metadata6} = pop(),
?assertMatch([{d, delta}, {g, gamma}|_], Metadata6),
?assertEqual("format world", lists:flatten(Message6)),
ok
end
},
{"log messages below the threshold are ignored",
fun() ->
?assertEqual(0, count()),

Carregando…
Cancelar
Guardar