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.

182 line
6.4 KiB

14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
14 年之前
  1. %% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved.
  2. %%
  3. %% This file is provided to you under the Apache License,
  4. %% Version 2.0 (the "License"); you may not use this file
  5. %% except in compliance with the License. You may obtain
  6. %% a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing,
  11. %% software distributed under the License is distributed on an
  12. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. %% KIND, either express or implied. See the License for the
  14. %% specific language governing permissions and limitations
  15. %% under the License.
  16. -module(lager_test_backend).
  17. -behaviour(gen_event).
  18. -export([init/1, handle_call/2, handle_event/2, handle_info/2, terminate/2,
  19. code_change/3]).
  20. -record(state, {level, buffer, ignored}).
  21. -compile([{parse_transform, lager_transform}]).
  22. -ifdef(TEST).
  23. -include_lib("eunit/include/eunit.hrl").
  24. -endif.
  25. init([Level]) ->
  26. {ok, #state{level=lager_util:level_to_num(Level), buffer=[], ignored=[]}}.
  27. handle_call(count, #state{buffer=Buffer} = State) ->
  28. {ok, length(Buffer), State};
  29. handle_call(count_ignored, #state{ignored=Ignored} = State) ->
  30. {ok, length(Ignored), State};
  31. handle_call(pop, #state{buffer=Buffer} = State) ->
  32. case Buffer of
  33. [] ->
  34. {ok, undefined, State};
  35. [H|T] ->
  36. {ok, H, State#state{buffer=T}}
  37. end;
  38. handle_call(get_loglevel, #state{level=Level} = State) ->
  39. {ok, Level, State};
  40. handle_call({set_loglevel, Level}, State) ->
  41. {ok, ok, State#state{level=lager_util:level_to_num(Level)}};
  42. handle_call(_Request, State) ->
  43. {ok, ok, State}.
  44. handle_event({log, Level, Time, Message}, #state{level=LogLevel,
  45. buffer=Buffer} = State) when Level >= LogLevel ->
  46. {ok, State#state{buffer=Buffer ++ [{Level, Time, Message}]}};
  47. handle_event({log, Level, Time, Message}, #state{ignored=Ignored} = State) ->
  48. {ok, State#state{ignored=Ignored ++ [ignored]}};
  49. handle_event(_Event, State) ->
  50. {ok, State}.
  51. handle_info(_Info, State) ->
  52. {ok, State}.
  53. terminate(_Reason, _State) ->
  54. ok.
  55. code_change(_OldVsn, State, _Extra) ->
  56. {ok, State}.
  57. -ifdef(TEST).
  58. pop() ->
  59. gen_event:call(lager_event, ?MODULE, pop).
  60. count() ->
  61. gen_event:call(lager_event, ?MODULE, count).
  62. count_ignored() ->
  63. gen_event:call(lager_event, ?MODULE, count_ignored).
  64. lager_test_() ->
  65. {foreach,
  66. fun setup/0,
  67. fun cleanup/1,
  68. [
  69. {"observe that there is nothing up my sleeve",
  70. fun() ->
  71. ?assertEqual(undefined, pop()),
  72. ?assertEqual(0, count())
  73. end
  74. },
  75. {"logging works",
  76. fun() ->
  77. lager:warning("test message"),
  78. ?assertEqual(1, count()),
  79. {Level, Time, Message} = pop(),
  80. ?assertMatch(Level, lager_util:level_to_num(warning)),
  81. [LevelStr, LocStr, MsgStr] = re:split(Message, " ", [{return, list}, {parts, 3}]),
  82. ?assertEqual("[warning]", LevelStr),
  83. ?assertEqual("test message", MsgStr),
  84. ok
  85. end
  86. },
  87. {"logging with arguments works",
  88. fun() ->
  89. lager:warning("test message ~p", [self()]),
  90. ?assertEqual(1, count()),
  91. {Level, Time, Message} = pop(),
  92. ?assertMatch(Level, lager_util:level_to_num(warning)),
  93. [LevelStr, LocStr, MsgStr] = re:split(Message, " ", [{return, list}, {parts, 3}]),
  94. ?assertEqual("[warning]", LevelStr),
  95. ?assertEqual(lists:flatten(io_lib:format("test message ~p", [self()])), MsgStr),
  96. ok
  97. end
  98. },
  99. {"logging works from inside a begin/end block",
  100. fun() ->
  101. ?assertEqual(0, count()),
  102. begin
  103. lager:warning("test message 2")
  104. end,
  105. ?assertEqual(1, count()),
  106. ok
  107. end
  108. },
  109. {"logging works from inside a list comprehension",
  110. fun() ->
  111. ?assertEqual(0, count()),
  112. [lager:warning("test message") || N <- lists:seq(1, 10)],
  113. ?assertEqual(10, count()),
  114. ok
  115. end
  116. },
  117. {"logging works from a begin/end block inside a list comprehension",
  118. fun() ->
  119. ?assertEqual(0, count()),
  120. [ begin lager:warning("test message") end || N <- lists:seq(1, 10)],
  121. ?assertEqual(10, count()),
  122. ok
  123. end
  124. },
  125. {"logging works from a nested list comprehension",
  126. fun() ->
  127. ?assertEqual(0, count()),
  128. [ [lager:warning("test message") || N <- lists:seq(1, 10)] ||
  129. I <- lists:seq(1, 10)],
  130. ?assertEqual(100, count()),
  131. ok
  132. end
  133. },
  134. {"log messages below the threshold are ignored",
  135. fun() ->
  136. ?assertEqual(0, count()),
  137. lager:debug("this message will be ignored"),
  138. ?assertEqual(0, count()),
  139. ?assertEqual(0, count_ignored()),
  140. lager_mochiglobal:put(loglevel, 0),
  141. lager:debug("this message should be ignored"),
  142. ?assertEqual(0, count()),
  143. ?assertEqual(1, count_ignored()),
  144. lager:set_loglevel(?MODULE, debug),
  145. lager:debug("this message should be logged"),
  146. ?assertEqual(1, count()),
  147. ?assertEqual(1, count_ignored()),
  148. ?assertEqual(debug, lager:get_loglevel(?MODULE)),
  149. ok
  150. end
  151. }
  152. ]
  153. }.
  154. setup() ->
  155. application:load(lager),
  156. application:set_env(lager, handlers, [{?MODULE, [info]}]),
  157. application:start(lager).
  158. cleanup(_) ->
  159. application:stop(lager),
  160. application:unload(lager).
  161. -endif.