You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 regels
3.8 KiB

Fix path expansion for traces when log_root is set Previously the following configuration didn't work properly: {log_root, "logs"}, {handlers, [{lager_file_backend, [{file, "access.log"}, {level, none}, ...]}]}, {traces, [{{lager_file_backend, "access.log"}, [{tag, access}], info}]} When lager:trace_file was trying to find a file among the existing handlers, it was searching for the expanded file name ("logs/access.log"), but the handlers store the file names without expansion ("access.log"), so it didn't find it. The result was that lager:trace_file started another "access.log" backend, which of course didn't use e.g. the log rotation settings of the first handler. When the log_root was relative, then each log message appeared twice in the log file (both backends thought that they should log it, because when calculating the Destination, "logs/access.log" was used in both cases). Now the code performs path expansion on the file name inside the handler too. Also, now it calculates the absolute paths of both files, so that if one file is specified with an absolute path and the other with a relative path, lager will still know that they are the same. Test ---- Using the old code (tag 3.2.4 was tested) the new test usually fails the following way: module 'lager_trace_test' lager_trace_test: trace_test_ (Trace combined with log_root)...*failed* in function lager_trace_test:'-trace_test_/0-fun-6-'/0 (test/lager_trace_test.erl, line 71) **throw:{too_many_entries,{ok,<<"Test message\n2017-03-26 23:04:23.935 [info] <0.5898.0>@lager"...>>}} output:<<"">> The reason for failing only usually is that it might happen that the duplicate log entry is added to file only after we called time:sleep(1000) and checked that the log entry is not there.
8 jaren geleden
  1. -module(lager_trace_test).
  2. -compile([{parse_transform, lager_transform}]).
  3. -ifdef(TEST).
  4. -include_lib("eunit/include/eunit.hrl").
  5. % Our expectation is that the first log entry will appear so we won't actually
  6. % wait out ?FIRST_LOG_ENTRY_TIMEOUT. On the other hand, the second log entry is
  7. % expected never to arrive, so the test will wait out ?SECOND_LOG_ENTRY_TIMEOUT;
  8. % that's why it is shorter.
  9. -define(FIRST_LOG_ENTRY_TIMEOUT, (10 * 1000)). % 10 seconds
  10. -define(SECOND_LOG_ENTRY_TIMEOUT, 1000). % 1 second
  11. -define(FNAME, "test/test1.log").
  12. trace_test_() ->
  13. {timeout,
  14. 10,
  15. {foreach,
  16. fun() ->
  17. file:write_file(?FNAME, ""),
  18. error_logger:tty(false),
  19. application:load(lager),
  20. application:set_env(lager, log_root, "test"),
  21. application:set_env(lager, handlers,
  22. [{lager_file_backend,
  23. [{file, "test1.log"},
  24. {level, none},
  25. {formatter, lager_default_formatter},
  26. {formatter_config, [message, "\n"]}
  27. ]}]),
  28. application:set_env(lager, traces,
  29. [{{lager_file_backend, "test1.log"},
  30. [{tag, mytag}], info}]),
  31. application:set_env(lager, error_logger_redirect, false),
  32. application:set_env(lager, async_threshold, undefined),
  33. lager:start()
  34. end,
  35. fun(_) ->
  36. file:delete(?FNAME),
  37. application:stop(lager),
  38. application:stop(goldrush),
  39. application:unset_env(lager, log_root),
  40. application:unset_env(lager, handlers),
  41. application:unset_env(lager, traces),
  42. application:unset_env(lager, error_logger_redirect),
  43. application:unset_env(lager, async_threshold),
  44. error_logger:tty(true)
  45. end,
  46. [{"Trace combined with log_root",
  47. fun() ->
  48. lager:info([{tag, mytag}], "Test message"),
  49. % Wait until we have the expected log entry in the log file.
  50. case wait_until(fun() ->
  51. count_lines(?FNAME) >= 1
  52. end, ?FIRST_LOG_ENTRY_TIMEOUT) of
  53. ok ->
  54. ok;
  55. {error, timeout} ->
  56. throw({file_empty, file:read_file(?FNAME)})
  57. end,
  58. % Let's wait a little to see that we don't get a duplicate log
  59. % entry.
  60. case wait_until(fun() ->
  61. count_lines(?FNAME) >= 2
  62. end, ?SECOND_LOG_ENTRY_TIMEOUT) of
  63. ok ->
  64. throw({too_many_entries, file:read_file(?FNAME)});
  65. {error, timeout} ->
  66. ok
  67. end
  68. end}
  69. ]}}.
  70. % Wait until Fun() returns true.
  71. wait_until(Fun, Timeout) ->
  72. wait_until(Fun, Timeout, {8, 13}).
  73. wait_until(_Fun, Timeout, {T1, _}) when T1 > Timeout ->
  74. {error, timeout};
  75. wait_until(Fun, Timeout, {T1, T2}) ->
  76. case Fun() of
  77. true ->
  78. ok;
  79. false ->
  80. timer:sleep(T1),
  81. wait_until(Fun, Timeout, {T2, T1 + T2})
  82. end.
  83. % Return the number of lines in a file. Return 0 for a non-existent file.
  84. count_lines(Filename) ->
  85. case file:read_file(Filename) of
  86. {ok, Content} ->
  87. Lines = binary:split(Content, <<"\n">>, [global, trim]),
  88. length(Lines);
  89. {error, _} ->
  90. 0
  91. end.
  92. -endif.