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.

1872 lines
95 KiB

преди 10 години
преди 10 години
преди 14 години
преди 10 години
преди 10 години
преди 10 години
преди 14 години
преди 10 години
преди 14 години
преди 13 години
преди 10 години
преди 13 години
преди 10 години
преди 10 години
преди 9 години
преди 14 години
преди 9 години
преди 14 години
преди 9 години
преди 8 години
преди 9 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
преди 13 години
преди 13 години
преди 12 години
преди 13 години
преди 12 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 11 години
преди 8 години
преди 11 години
преди 9 години
преди 13 години
преди 13 години
преди 13 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 8 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 9 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 13 години
преди 9 години
преди 11 години
преди 14 години
преди 13 години
преди 14 години
преди 9 години
преди 9 години
преди 9 години
преди 10 години
преди 9 години
преди 9 години
преди 14 години
преди 9 години
преди 14 години
преди 9 години
преди 9 години
преди 9 години
преди 14 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 13 години
преди 9 години
преди 9 години
преди 13 години
преди 9 години
преди 9 години
преди 10 години
преди 9 години
преди 9 години
преди 10 години
преди 9 години
преди 9 години
преди 10 години
преди 9 години
преди 9 години
преди 10 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 12 години
преди 8 години
преди 8 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
  1. %% -------------------------------------------------------------------
  2. %%
  3. %% Copyright (c) 2011-2017 Basho Technologies, Inc.
  4. %%
  5. %% This file is provided to you under the Apache License,
  6. %% Version 2.0 (the "License"); you may not use this file
  7. %% except in compliance with the License. You may obtain
  8. %% a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing,
  13. %% software distributed under the License is distributed on an
  14. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. %% KIND, either express or implied. See the License for the
  16. %% specific language governing permissions and limitations
  17. %% under the License.
  18. %%
  19. %% -------------------------------------------------------------------
  20. -module(lager_test_backend).
  21. -behaviour(gen_event).
  22. -export([init/1, handle_call/2, handle_event/2, handle_info/2, terminate/2,
  23. code_change/3]).
  24. -include("lager.hrl").
  25. -define(TEST_SINK_NAME, '__lager_test_sink'). %% <-- used by parse transform
  26. -define(TEST_SINK_EVENT, '__lager_test_sink_lager_event'). %% <-- used by lager API calls and internals for gen_event
  27. -record(state, {
  28. level :: list(),
  29. buffer :: list(),
  30. ignored :: term()
  31. }).
  32. -compile({parse_transform, lager_transform}).
  33. -ifdef(TEST).
  34. -include_lib("eunit/include/eunit.hrl").
  35. -record(test, {
  36. attrs :: list(),
  37. format :: list(),
  38. args :: list()
  39. }).
  40. -export([
  41. count/0,
  42. count_ignored/0,
  43. flush/0,
  44. message_stuffer/3,
  45. pop/0,
  46. print_state/0
  47. ]).
  48. -endif.
  49. init(Level) ->
  50. {ok, #state{level=lager_util:config_to_mask(Level), buffer=[], ignored=[]}}.
  51. handle_call(count, #state{buffer=Buffer} = State) ->
  52. {ok, length(Buffer), State};
  53. handle_call(count_ignored, #state{ignored=Ignored} = State) ->
  54. {ok, length(Ignored), State};
  55. handle_call(flush, State) ->
  56. {ok, ok, State#state{buffer=[], ignored=[]}};
  57. handle_call(pop, #state{buffer=Buffer} = State) ->
  58. case Buffer of
  59. [] ->
  60. {ok, undefined, State};
  61. [H|T] ->
  62. {ok, H, State#state{buffer=T}}
  63. end;
  64. handle_call(get_loglevel, #state{level=Level} = State) ->
  65. {ok, Level, State};
  66. handle_call({set_loglevel, Level}, State) ->
  67. {ok, ok, State#state{level=lager_util:config_to_mask(Level)}};
  68. handle_call(print_state, State) ->
  69. spawn(fun() -> lager:info("State ~p", [lager:pr(State, ?MODULE)]) end),
  70. timer:sleep(100),
  71. {ok, ok, State};
  72. handle_call(print_bad_state, State) ->
  73. spawn(fun() -> lager:info("State ~p", [lager:pr({state, 1}, ?MODULE)]) end),
  74. timer:sleep(100),
  75. {ok, ok, State};
  76. handle_call(_Request, State) ->
  77. {ok, ok, State}.
  78. handle_event({log, Msg},
  79. #state{level=LogLevel,buffer=Buffer,ignored=Ignored} = State) ->
  80. case lager_util:is_loggable(Msg, LogLevel, ?MODULE) of
  81. true ->
  82. {ok, State#state{buffer=Buffer ++
  83. [{lager_msg:severity_as_int(Msg),
  84. lager_msg:datetime(Msg),
  85. lager_msg:message(Msg), lager_msg:metadata(Msg)}]}};
  86. _ ->
  87. {ok, State#state{ignored=Ignored ++ [ignored]}}
  88. end;
  89. handle_event(_Event, State) ->
  90. {ok, State}.
  91. handle_info(_Info, State) ->
  92. {ok, State}.
  93. terminate(_Reason, _State) ->
  94. ok.
  95. code_change(_OldVsn, State, _Extra) ->
  96. {ok, State}.
  97. -ifdef(TEST).
  98. pop() ->
  99. pop(lager_event).
  100. count() ->
  101. count(lager_event).
  102. count_ignored() ->
  103. count_ignored(lager_event).
  104. flush() ->
  105. flush(lager_event).
  106. print_state() ->
  107. print_state(lager_event).
  108. print_bad_state() ->
  109. print_bad_state(lager_event).
  110. pop(Sink) ->
  111. gen_event:call(Sink, ?MODULE, pop).
  112. count(Sink) ->
  113. gen_event:call(Sink, ?MODULE, count).
  114. count_ignored(Sink) ->
  115. gen_event:call(Sink, ?MODULE, count_ignored).
  116. flush(Sink) ->
  117. gen_event:call(Sink, ?MODULE, flush).
  118. print_state(Sink) ->
  119. gen_event:call(Sink, ?MODULE, print_state).
  120. print_bad_state(Sink) ->
  121. gen_event:call(Sink, ?MODULE, print_bad_state).
  122. has_line_numbers() ->
  123. %% are we R15 or greater
  124. % this gets called a LOT - cache the answer
  125. case erlang:get({?MODULE, has_line_numbers}) of
  126. undefined ->
  127. R = lager_util:otp_version() >= 15,
  128. erlang:put({?MODULE, has_line_numbers}, R),
  129. R;
  130. Bool ->
  131. Bool
  132. end.
  133. not_running_test() ->
  134. ?assertEqual({error, lager_not_running}, lager:log(info, self(), "not running")).
  135. lager_test_() ->
  136. {foreach,
  137. fun setup/0,
  138. fun cleanup/1,
  139. [
  140. {"observe that there is nothing up my sleeve",
  141. fun() ->
  142. ?assertEqual(undefined, pop()),
  143. ?assertEqual(0, count())
  144. end
  145. },
  146. {"test sink not running",
  147. fun() ->
  148. ?assertEqual({error, {sink_not_configured, test}}, lager:log(test, info, self(), "~p", "not running"))
  149. end
  150. },
  151. {"logging works",
  152. fun() ->
  153. lager:warning("test message"),
  154. ?assertEqual(1, count()),
  155. {Level, _Time, Message, _Metadata} = pop(),
  156. ?assertMatch(Level, lager_util:level_to_num(warning)),
  157. ?assertEqual("test message", Message),
  158. ok
  159. end
  160. },
  161. {"logging with macro works",
  162. fun() ->
  163. ?warning("test message", []),
  164. ?assertEqual(1, count()),
  165. {Level, _Time, Message, _Metadata} = pop(),
  166. ?assertMatch(Level, lager_util:level_to_num(warning)),
  167. ?assertEqual("test message", Message),
  168. ok
  169. end
  170. },
  171. {"unsafe logging works",
  172. fun() ->
  173. lager:warning_unsafe("test message"),
  174. ?assertEqual(1, count()),
  175. {Level, _Time, Message, _Metadata} = pop(),
  176. ?assertMatch(Level, lager_util:level_to_num(warning)),
  177. ?assertEqual("test message", Message),
  178. ok
  179. end
  180. },
  181. {"logging with arguments works",
  182. fun() ->
  183. lager:warning("test message ~p", [self()]),
  184. ?assertEqual(1, count()),
  185. {Level, _Time, Message,_Metadata} = pop(),
  186. ?assertMatch(Level, lager_util:level_to_num(warning)),
  187. ?assertEqual(lists:flatten(io_lib:format("test message ~p", [self()])), lists:flatten(Message)),
  188. ok
  189. end
  190. },
  191. {"logging with macro and arguments works",
  192. fun() ->
  193. ?warning("test message ~p", [self()]),
  194. ?assertEqual(1, count()),
  195. {Level, _Time, Message,_Metadata} = pop(),
  196. ?assertMatch(Level, lager_util:level_to_num(warning)),
  197. ?assertEqual(lists:flatten(io_lib:format("test message ~p", [self()])), lists:flatten(Message)),
  198. ok
  199. end
  200. },
  201. {"unsafe logging with args works",
  202. fun() ->
  203. lager:warning_unsafe("test message ~p", [self()]),
  204. ?assertEqual(1, count()),
  205. {Level, _Time, Message,_Metadata} = pop(),
  206. ?assertMatch(Level, lager_util:level_to_num(warning)),
  207. ?assertEqual(lists:flatten(io_lib:format("test message ~p", [self()])), lists:flatten(Message)),
  208. ok
  209. end
  210. },
  211. {"logging works from inside a begin/end block",
  212. fun() ->
  213. ?assertEqual(0, count()),
  214. begin
  215. lager:warning("test message 2")
  216. end,
  217. ?assertEqual(1, count()),
  218. ok
  219. end
  220. },
  221. {"logging works from inside a list comprehension",
  222. fun() ->
  223. ?assertEqual(0, count()),
  224. [lager:warning("test message") || _N <- lists:seq(1, 10)],
  225. ?assertEqual(10, count()),
  226. ok
  227. end
  228. },
  229. {"logging works from a begin/end block inside a list comprehension",
  230. fun() ->
  231. ?assertEqual(0, count()),
  232. [ begin lager:warning("test message") end || _N <- lists:seq(1, 10)],
  233. ?assertEqual(10, count()),
  234. ok
  235. end
  236. },
  237. {"logging works from a nested list comprehension",
  238. fun() ->
  239. ?assertEqual(0, count()),
  240. [ [lager:warning("test message") || _N <- lists:seq(1, 10)] ||
  241. _I <- lists:seq(1, 10)],
  242. ?assertEqual(100, count()),
  243. ok
  244. end
  245. },
  246. {"logging with only metadata works",
  247. fun() ->
  248. ?assertEqual(0, count()),
  249. lager:warning([{just, metadata}]),
  250. lager:warning([{just, metadata}, {foo, bar}]),
  251. ?assertEqual(2, count()),
  252. ok
  253. end
  254. },
  255. {"variables inplace of literals in logging statements work",
  256. fun() ->
  257. ?assertEqual(0, count()),
  258. Attr = [{a, alpha}, {b, beta}],
  259. Fmt = "format ~p",
  260. Args = [world],
  261. lager:info(Attr, "hello"),
  262. lager:info(Attr, "hello ~p", [world]),
  263. lager:info(Fmt, [world]),
  264. lager:info("hello ~p", Args),
  265. lager:info(Attr, "hello ~p", Args),
  266. lager:info([{d, delta}, {g, gamma}], Fmt, Args),
  267. ?assertEqual(6, count()),
  268. {_Level, _Time, Message, Metadata} = pop(),
  269. ?assertMatch([{a, alpha}, {b, beta}|_], Metadata),
  270. ?assertEqual("hello", lists:flatten(Message)),
  271. {_Level, _Time2, Message2, _Metadata2} = pop(),
  272. ?assertEqual("hello world", lists:flatten(Message2)),
  273. {_Level, _Time3, Message3, _Metadata3} = pop(),
  274. ?assertEqual("format world", lists:flatten(Message3)),
  275. {_Level, _Time4, Message4, _Metadata4} = pop(),
  276. ?assertEqual("hello world", lists:flatten(Message4)),
  277. {_Level, _Time5, Message5, _Metadata5} = pop(),
  278. ?assertEqual("hello world", lists:flatten(Message5)),
  279. {_Level, _Time6, Message6, Metadata6} = pop(),
  280. ?assertMatch([{d, delta}, {g, gamma}|_], Metadata6),
  281. ?assertEqual("format world", lists:flatten(Message6)),
  282. ok
  283. end
  284. },
  285. {"list comprehension inplace of literals in logging statements work",
  286. fun() ->
  287. ?assertEqual(0, count()),
  288. Attr = [{a, alpha}, {b, beta}],
  289. Fmt = "format ~p",
  290. Args = [world],
  291. lager:info([{K, atom_to_list(V)} || {K, V} <- Attr], "hello"),
  292. lager:info([{K, atom_to_list(V)} || {K, V} <- Attr], "hello ~p", [{atom, X} || X <- Args]),
  293. lager:info([X || X <- Fmt], [world]),
  294. lager:info("hello ~p", [{atom, X} || X <- Args]),
  295. lager:info([{K, atom_to_list(V)} || {K, V} <- Attr], "hello ~p", [{atom, X} || X <- Args]),
  296. lager:info([{d, delta}, {g, gamma}], Fmt, [{atom, X} || X <- Args]),
  297. ?assertEqual(6, count()),
  298. {_Level, _Time, Message, Metadata} = pop(),
  299. ?assertMatch([{a, "alpha"}, {b, "beta"}|_], Metadata),
  300. ?assertEqual("hello", lists:flatten(Message)),
  301. {_Level, _Time2, Message2, _Metadata2} = pop(),
  302. ?assertEqual("hello {atom,world}", lists:flatten(Message2)),
  303. {_Level, _Time3, Message3, _Metadata3} = pop(),
  304. ?assertEqual("format world", lists:flatten(Message3)),
  305. {_Level, _Time4, Message4, _Metadata4} = pop(),
  306. ?assertEqual("hello {atom,world}", lists:flatten(Message4)),
  307. {_Level, _Time5, Message5, _Metadata5} = pop(),
  308. ?assertEqual("hello {atom,world}", lists:flatten(Message5)),
  309. {_Level, _Time6, Message6, Metadata6} = pop(),
  310. ?assertMatch([{d, delta}, {g, gamma}|_], Metadata6),
  311. ?assertEqual("format {atom,world}", lists:flatten(Message6)),
  312. ok
  313. end
  314. },
  315. {"function calls inplace of literals in logging statements work",
  316. fun() ->
  317. ?assertEqual(0, count()),
  318. put(attrs, [{a, alpha}, {b, beta}]),
  319. put(format, "format ~p"),
  320. put(args, [world]),
  321. lager:info(get(attrs), "hello"),
  322. lager:info(get(attrs), "hello ~p", get(args)),
  323. lager:info(get(format), [world]),
  324. lager:info("hello ~p", erlang:get(args)),
  325. lager:info(fun() -> get(attrs) end(), "hello ~p", get(args)),
  326. lager:info([{d, delta}, {g, gamma}], get(format), get(args)),
  327. ?assertEqual(6, count()),
  328. {_Level, _Time, Message, Metadata} = pop(),
  329. ?assertMatch([{a, alpha}, {b, beta}|_], Metadata),
  330. ?assertEqual("hello", lists:flatten(Message)),
  331. {_Level, _Time2, Message2, _Metadata2} = pop(),
  332. ?assertEqual("hello world", lists:flatten(Message2)),
  333. {_Level, _Time3, Message3, _Metadata3} = pop(),
  334. ?assertEqual("format world", lists:flatten(Message3)),
  335. {_Level, _Time4, Message4, _Metadata4} = pop(),
  336. ?assertEqual("hello world", lists:flatten(Message4)),
  337. {_Level, _Time5, Message5, _Metadata5} = pop(),
  338. ?assertEqual("hello world", lists:flatten(Message5)),
  339. {_Level, _Time6, Message6, Metadata6} = pop(),
  340. ?assertMatch([{d, delta}, {g, gamma}|_], Metadata6),
  341. ?assertEqual("format world", lists:flatten(Message6)),
  342. ok
  343. end
  344. },
  345. {"record fields inplace of literals in logging statements work",
  346. fun() ->
  347. ?assertEqual(0, count()),
  348. Test = #test{attrs=[{a, alpha}, {b, beta}], format="format ~p", args=[world]},
  349. lager:info(Test#test.attrs, "hello"),
  350. lager:info(Test#test.attrs, "hello ~p", Test#test.args),
  351. lager:info(Test#test.format, [world]),
  352. lager:info("hello ~p", Test#test.args),
  353. lager:info(Test#test.attrs, "hello ~p", Test#test.args),
  354. lager:info([{d, delta}, {g, gamma}], Test#test.format, Test#test.args),
  355. ?assertEqual(6, count()),
  356. {_Level, _Time, Message, Metadata} = pop(),
  357. ?assertMatch([{a, alpha}, {b, beta}|_], Metadata),
  358. ?assertEqual("hello", lists:flatten(Message)),
  359. {_Level, _Time2, Message2, _Metadata2} = pop(),
  360. ?assertEqual("hello world", lists:flatten(Message2)),
  361. {_Level, _Time3, Message3, _Metadata3} = pop(),
  362. ?assertEqual("format world", lists:flatten(Message3)),
  363. {_Level, _Time4, Message4, _Metadata4} = pop(),
  364. ?assertEqual("hello world", lists:flatten(Message4)),
  365. {_Level, _Time5, Message5, _Metadata5} = pop(),
  366. ?assertEqual("hello world", lists:flatten(Message5)),
  367. {_Level, _Time6, Message6, Metadata6} = pop(),
  368. ?assertMatch([{d, delta}, {g, gamma}|_], Metadata6),
  369. ?assertEqual("format world", lists:flatten(Message6)),
  370. ok
  371. end
  372. },
  373. {"log messages below the threshold are ignored",
  374. fun() ->
  375. ?assertEqual(0, count()),
  376. lager:debug("this message will be ignored"),
  377. ?assertEqual(0, count()),
  378. ?assertEqual(0, count_ignored()),
  379. lager_config:set(loglevel, {element(2, lager_util:config_to_mask(debug)), []}),
  380. lager:debug("this message should be ignored"),
  381. ?assertEqual(0, count()),
  382. ?assertEqual(1, count_ignored()),
  383. lager:set_loglevel(?MODULE, debug),
  384. ?assertEqual({?DEBUG bor ?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)),
  385. lager:debug("this message should be logged"),
  386. ?assertEqual(1, count()),
  387. ?assertEqual(1, count_ignored()),
  388. ?assertEqual(debug, lager:get_loglevel(?MODULE)),
  389. ok
  390. end
  391. },
  392. {"tracing works",
  393. fun() ->
  394. lager_config:set(loglevel, {element(2, lager_util:config_to_mask(error)), []}),
  395. ok = lager:info("hello world"),
  396. ?assertEqual(0, count()),
  397. lager:trace(?MODULE, [{module, ?MODULE}], debug),
  398. ?assertMatch({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, _}, lager_config:get(loglevel)),
  399. %% elegible for tracing
  400. ok = lager:info("hello world"),
  401. %% NOT elegible for tracing
  402. ok = lager:log(info, [{pid, self()}], "hello world"),
  403. ?assertEqual(1, count()),
  404. ok
  405. end
  406. },
  407. {"tracing works with custom attributes",
  408. fun() ->
  409. lager:set_loglevel(?MODULE, error),
  410. ?assertEqual({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)),
  411. lager_config:set(loglevel, {element(2, lager_util:config_to_mask(error)), []}),
  412. lager:info([{requestid, 6}], "hello world"),
  413. ?assertEqual(0, count()),
  414. lager:trace(?MODULE, [{requestid, 6}, {foo, bar}], debug),
  415. lager:info([{requestid, 6}, {foo, bar}], "hello world"),
  416. ?assertEqual(1, count()),
  417. lager:trace(?MODULE, [{requestid, '*'}], debug),
  418. lager:info([{requestid, 6}], "hello world"),
  419. ?assertEqual(2, count()),
  420. lager:clear_all_traces(),
  421. lager:info([{requestid, 6}], "hello world"),
  422. ?assertEqual(2, count()),
  423. ok
  424. end
  425. },
  426. {"tracing works with custom attributes and event stream processing",
  427. fun() ->
  428. lager:set_loglevel(?MODULE, error),
  429. ?assertEqual({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)),
  430. lager_config:set(loglevel, {element(2, lager_util:config_to_mask(error)), []}),
  431. lager:info([{requestid, 6}], "hello world"),
  432. ?assertEqual(0, count()),
  433. lager:trace(?MODULE, [{requestid, '>', 5}, {requestid, '<', 7}, {foo, bar}], debug),
  434. lager:info([{requestid, 5}, {foo, bar}], "hello world"),
  435. lager:info([{requestid, 6}, {foo, bar}], "hello world"),
  436. ?assertEqual(1, count()),
  437. lager:clear_all_traces(),
  438. lager:trace(?MODULE, [{requestid, '>', 8}, {foo, bar}]),
  439. lager:info([{foo, bar}], "hello world"),
  440. lager:info([{requestid, 6}], "hello world"),
  441. lager:info([{requestid, 7}], "hello world"),
  442. lager:info([{requestid, 8}], "hello world"),
  443. lager:info([{requestid, 9}, {foo, bar}], "hello world"),
  444. lager:info([{requestid, 10}], "hello world"),
  445. ?assertEqual(2, count()),
  446. lager:trace(?MODULE, [{requestid, '>', 8}]),
  447. lager:info([{foo, bar}], "hello world"),
  448. lager:info([{requestid, 6}], "hello world"),
  449. lager:info([{requestid, 7}], "hello world"),
  450. lager:info([{requestid, 8}], "hello world"),
  451. lager:info([{requestid, 9}, {foo, bar}], "hello world"),
  452. lager:info([{requestid, 10}], "hello world"),
  453. ?assertEqual(4, count()),
  454. lager:trace(?MODULE, [{foo, '=', bar}]),
  455. lager:info([{foo, bar}], "hello world"),
  456. lager:info([{requestid, 6}], "hello world"),
  457. lager:info([{requestid, 7}], "hello world"),
  458. lager:info([{requestid, 8}], "hello world"),
  459. lager:info([{requestid, 9}, {foo, bar}], "hello world"),
  460. lager:info([{requestid, 10}], "hello world"),
  461. lager:trace(?MODULE, [{fu, '!'}]),
  462. lager:info([{foo, bar}], "hello world"),
  463. lager:info([{ooh, car}], "hello world"),
  464. lager:info([{fu, bar}], "hello world"),
  465. lager:trace(?MODULE, [{fu, '*'}]),
  466. lager:info([{fu, bar}], "hello world"),
  467. ?assertEqual(10, count()),
  468. lager:clear_all_traces(),
  469. lager:info([{requestid, 6}], "hello world"),
  470. ?assertEqual(10, count()),
  471. lager:clear_all_traces(),
  472. lager:trace(?MODULE, [{requestid, '>=', 5}, {requestid, '=<', 7}], debug),
  473. lager:info([{requestid, 4}], "nope!"),
  474. lager:info([{requestid, 5}], "hello world"),
  475. lager:info([{requestid, 7}], "hello world again"),
  476. ?assertEqual(12, count()),
  477. lager:clear_all_traces(),
  478. lager:trace(?MODULE, [{foo, '!=', bar}]),
  479. lager:info([{foo, bar}], "hello world"),
  480. ?assertEqual(12, count()),
  481. lager:info([{foo, baz}], "blarg"),
  482. ?assertEqual(13, count()),
  483. lager:clear_all_traces(),
  484. lager:trace(?MODULE, [{all, [{foo, '=', bar}, {null, false}]}]),
  485. lager:info([{foo, bar}], "should not be logged"),
  486. ?assertEqual(13, count()),
  487. lager:clear_all_traces(),
  488. lager:trace(?MODULE, [{any, [{foo, '=', bar}, {null, true}]}]),
  489. lager:info([{foo, qux}], "should be logged"),
  490. ?assertEqual(14, count()),
  491. ok
  492. end
  493. },
  494. {"tracing custom attributes works with event stream processing statistics and reductions",
  495. fun() ->
  496. lager:set_loglevel(?MODULE, error),
  497. ?assertEqual({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)),
  498. lager_config:set(loglevel, {element(2, lager_util:config_to_mask(error)), []}),
  499. lager:info([{requestid, 6}], "hello world"),
  500. ?assertEqual(0, count()),
  501. lager:trace(?MODULE, [{beta, '*'}]),
  502. lager:trace(?MODULE, [{meta, "data"}]),
  503. lager:info([{meta, "data"}], "hello world"),
  504. lager:info([{beta, 2}], "hello world"),
  505. lager:info([{beta, 2.1}, {foo, bar}], "hello world"),
  506. lager:info([{meta, <<"data">>}], "hello world"),
  507. ?assertEqual(8, ?DEFAULT_TRACER:info(input)),
  508. ?assertEqual(6, ?DEFAULT_TRACER:info(output)),
  509. ?assertEqual(2, ?DEFAULT_TRACER:info(filter)),
  510. lager:clear_all_traces(),
  511. lager:trace(?MODULE, [{meta, "data"}]),
  512. lager:trace(?MODULE, [{beta, '>', 2}, {beta, '<', 2.12}]),
  513. lager:info([{meta, "data"}], "hello world"),
  514. lager:info([{beta, 2}], "hello world"),
  515. lager:info([{beta, 2.1}, {foo, bar}], "hello world"),
  516. lager:info([{meta, <<"data">>}], "hello world"),
  517. ?assertEqual(8, ?DEFAULT_TRACER:info(input)),
  518. ?assertEqual(4, ?DEFAULT_TRACER:info(output)),
  519. ?assertEqual(4, ?DEFAULT_TRACER:info(filter)),
  520. lager:clear_all_traces(),
  521. lager:trace_console([{beta, '>', 2}, {meta, "data"}]),
  522. lager:trace_console([{beta, '>', 2}, {beta, '<', 2.12}]),
  523. Reduced = {all,[{any,[{beta,'<',2.12},{meta,'=',"data"}]},
  524. {beta,'>',2}]},
  525. ?assertEqual(Reduced, ?DEFAULT_TRACER:info('query')),
  526. lager:clear_all_traces(),
  527. lager:info([{requestid, 6}], "hello world"),
  528. ?assertEqual(5, count()),
  529. ok
  530. end
  531. },
  532. {"persistent traces work",
  533. fun() ->
  534. ?assertEqual(0, count()),
  535. lager:debug([{foo, bar}], "hello world"),
  536. ?assertEqual(0, count()),
  537. application:stop(lager),
  538. application:set_env(lager, traces, [{lager_test_backend, [{foo, bar}], debug}]),
  539. lager:start(),
  540. timer:sleep(5),
  541. flush(),
  542. lager:debug([{foo, bar}], "hello world"),
  543. ?assertEqual(1, count()),
  544. application:unset_env(lager, traces),
  545. ok
  546. end
  547. },
  548. {"tracing honors loglevel",
  549. fun() ->
  550. lager:set_loglevel(?MODULE, error),
  551. ?assertEqual({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)),
  552. {ok, T} = lager:trace(?MODULE, [{module, ?MODULE}], notice),
  553. ok = lager:info("hello world"),
  554. ?assertEqual(0, count()),
  555. ok = lager:notice("hello world"),
  556. ?assertEqual(1, count()),
  557. lager:stop_trace(T),
  558. ok = lager:notice("hello world"),
  559. ?assertEqual(1, count()),
  560. ok
  561. end
  562. },
  563. {"stopped trace stops and removes its event handler - default sink (gh#267)",
  564. {timeout, 10,
  565. fun() ->
  566. Sink = ?DEFAULT_SINK,
  567. StartHandlers = gen_event:which_handlers(Sink),
  568. {_, T0} = lager_config:get({Sink, loglevel}),
  569. StartGlobal = lager_config:global_get(handlers),
  570. ?assertEqual([], T0),
  571. {ok, TestTrace1} = lager:trace_file("/tmp/test", [{a,b}]),
  572. MidHandlers = gen_event:which_handlers(Sink),
  573. {ok, TestTrace2} = lager:trace_file("/tmp/test", [{c,d}]),
  574. MidHandlers = gen_event:which_handlers(Sink),
  575. ?assertEqual(length(StartHandlers)+1, length(MidHandlers)),
  576. MidGlobal = lager_config:global_get(handlers),
  577. ?assertEqual(length(StartGlobal)+1, length(MidGlobal)),
  578. {_, T1} = lager_config:get({Sink, loglevel}),
  579. ?assertEqual(2, length(T1)),
  580. ok = lager:stop_trace(TestTrace1),
  581. {_, T2} = lager_config:get({Sink, loglevel}),
  582. ?assertEqual(1, length(T2)),
  583. ?assertEqual(length(StartHandlers)+1, length(
  584. gen_event:which_handlers(Sink))),
  585. ?assertEqual(length(StartGlobal)+1, length(lager_config:global_get(handlers))),
  586. ok = lager:stop_trace(TestTrace2),
  587. EndHandlers = gen_event:which_handlers(Sink),
  588. EndGlobal = lager_config:global_get(handlers),
  589. {_, T3} = lager_config:get({Sink, loglevel}),
  590. ?assertEqual([], T3),
  591. ?assertEqual(StartHandlers, EndHandlers),
  592. ?assertEqual(StartGlobal, EndGlobal),
  593. ok
  594. end}
  595. },
  596. {"record printing works",
  597. fun() ->
  598. print_state(),
  599. {Level, _Time, Message, _Metadata} = pop(),
  600. ?assertMatch(Level, lager_util:level_to_num(info)),
  601. {mask, Mask} = lager_util:config_to_mask(info),
  602. ?assertEqual("State #state{level={mask,"++integer_to_list(Mask)++"},buffer=[],ignored=[]}", lists:flatten(Message)),
  603. ok
  604. end
  605. },
  606. {"record printing fails gracefully",
  607. fun() ->
  608. print_bad_state(),
  609. {Level, _Time, Message, _Metadata} = pop(),
  610. ?assertMatch(Level, lager_util:level_to_num(info)),
  611. ?assertEqual("State {state,1}", lists:flatten(Message)),
  612. ok
  613. end
  614. },
  615. {"record printing fails gracefully when no lager_record attribute",
  616. fun() ->
  617. spawn(fun() -> lager:info("State ~p", [lager:pr({state, 1}, lager)]) end),
  618. timer:sleep(100),
  619. {Level, _Time, Message, _Metadata} = pop(),
  620. ?assertMatch(Level, lager_util:level_to_num(info)),
  621. ?assertEqual("State {state,1}", lists:flatten(Message)),
  622. ok
  623. end
  624. },
  625. {"record printing fails gracefully when input is not a tuple",
  626. fun() ->
  627. spawn(fun() -> lager:info("State ~p", [lager:pr(ok, lager)]) end),
  628. timer:sleep(100),
  629. {Level, _Time, Message, _Metadata} = pop(),
  630. ?assertMatch(Level, lager_util:level_to_num(info)),
  631. ?assertEqual("State ok", lists:flatten(Message)),
  632. ok
  633. end
  634. },
  635. {"record printing fails gracefully when module is invalid",
  636. fun() ->
  637. spawn(fun() -> lager:info("State ~p", [lager:pr({state, 1}, not_a_module)]) end),
  638. timer:sleep(1000),
  639. {Level, _Time, Message, _Metadata} = pop(),
  640. ?assertMatch(Level, lager_util:level_to_num(info)),
  641. ?assertEqual("State {state,1}", lists:flatten(Message)),
  642. ok
  643. end
  644. },
  645. {"installing a new handler adjusts the global loglevel if necessary",
  646. fun() ->
  647. ?assertEqual({?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)),
  648. supervisor:start_child(lager_handler_watcher_sup, [lager_event, {?MODULE, foo}, debug]),
  649. ?assertEqual({?DEBUG bor ?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)),
  650. ok
  651. end
  652. },
  653. {"metadata in the process dictionary works",
  654. fun() ->
  655. lager:md([{platypus, gravid}, {sloth, hirsute}, {duck, erroneous}]),
  656. lager:info("I sing the animal kingdom electric!"),
  657. {_Level, _Time, _Message, Metadata} = pop(),
  658. ?assertEqual(gravid, proplists:get_value(platypus, Metadata)),
  659. ?assertEqual(hirsute, proplists:get_value(sloth, Metadata)),
  660. ?assertEqual(erroneous, proplists:get_value(duck, Metadata)),
  661. ?assertEqual(undefined, proplists:get_value(eagle, Metadata)),
  662. lager:md([{platypus, gravid}, {sloth, hirsute}, {eagle, superincumbent}]),
  663. lager:info("I sing the animal kingdom dielectric!"),
  664. {_Level2, _Time2, _Message2, Metadata2} = pop(),
  665. ?assertEqual(gravid, proplists:get_value(platypus, Metadata2)),
  666. ?assertEqual(hirsute, proplists:get_value(sloth, Metadata2)),
  667. ?assertEqual(undefined, proplists:get_value(duck, Metadata2)),
  668. ?assertEqual(superincumbent, proplists:get_value(eagle, Metadata2)),
  669. ok
  670. end
  671. },
  672. {"unsafe messages really are not truncated",
  673. fun() ->
  674. lager:info_unsafe("doom, doom has come upon you all ~p", [string:copies("doom", 1500)]),
  675. {_, _, Msg,_Metadata} = pop(),
  676. ?assert(length(lists:flatten(Msg)) == 6035)
  677. end
  678. },
  679. {"can't store invalid metadata",
  680. fun() ->
  681. ?assertEqual(ok, lager:md([{platypus, gravid}, {sloth, hirsute}, {duck, erroneous}])),
  682. ?assertError(badarg, lager:md({flamboyant, flamingo})),
  683. ?assertError(badarg, lager:md("zookeeper zephyr")),
  684. ok
  685. end
  686. },
  687. {"dates should be local by default",
  688. fun() ->
  689. lager:warning("so long, and thanks for all the fish"),
  690. ?assertEqual(1, count()),
  691. {_Level, {_Date, Time}, _Message, _Metadata} = pop(),
  692. ?assertEqual(nomatch, binary:match(iolist_to_binary(Time), <<"UTC">>)),
  693. ok
  694. end
  695. },
  696. {"dates should be UTC if SASL is configured as UTC",
  697. fun() ->
  698. application:set_env(sasl, utc_log, true),
  699. lager:warning("so long, and thanks for all the fish"),
  700. application:set_env(sasl, utc_log, false),
  701. ?assertEqual(1, count()),
  702. {_Level, {_Date, Time}, _Message, _Metadata} = pop(),
  703. ?assertNotEqual(nomatch, binary:match(iolist_to_binary(Time), <<"UTC">>)),
  704. ok
  705. end
  706. }
  707. ]
  708. }.
  709. extra_sinks_test_() ->
  710. {foreach,
  711. fun setup_sink/0,
  712. fun cleanup/1,
  713. [
  714. {"observe that there is nothing up my sleeve",
  715. fun() ->
  716. ?assertEqual(undefined, pop(?TEST_SINK_EVENT)),
  717. ?assertEqual(0, count(?TEST_SINK_EVENT))
  718. end
  719. },
  720. {"logging works",
  721. fun() ->
  722. ?TEST_SINK_NAME:warning("test message"),
  723. ?assertEqual(1, count(?TEST_SINK_EVENT)),
  724. {Level, _Time, Message, _Metadata} = pop(?TEST_SINK_EVENT),
  725. ?assertMatch(Level, lager_util:level_to_num(warning)),
  726. ?assertEqual("test message", Message),
  727. ok
  728. end
  729. },
  730. {"logging with arguments works",
  731. fun() ->
  732. ?TEST_SINK_NAME:warning("test message ~p", [self()]),
  733. ?assertEqual(1, count(?TEST_SINK_EVENT)),
  734. {Level, _Time, Message,_Metadata} = pop(?TEST_SINK_EVENT),
  735. ?assertMatch(Level, lager_util:level_to_num(warning)),
  736. ?assertEqual(lists:flatten(io_lib:format("test message ~p", [self()])), lists:flatten(Message)),
  737. ok
  738. end
  739. },
  740. {"variables inplace of literals in logging statements work",
  741. fun() ->
  742. ?assertEqual(0, count(?TEST_SINK_EVENT)),
  743. Attr = [{a, alpha}, {b, beta}],
  744. Fmt = "format ~p",
  745. Args = [world],
  746. ?TEST_SINK_NAME:info(Attr, "hello"),
  747. ?TEST_SINK_NAME:info(Attr, "hello ~p", [world]),
  748. ?TEST_SINK_NAME:info(Fmt, [world]),
  749. ?TEST_SINK_NAME:info("hello ~p", Args),
  750. ?TEST_SINK_NAME:info(Attr, "hello ~p", Args),
  751. ?TEST_SINK_NAME:info([{d, delta}, {g, gamma}], Fmt, Args),
  752. ?assertEqual(6, count(?TEST_SINK_EVENT)),
  753. {_Level, _Time, Message, Metadata} = pop(?TEST_SINK_EVENT),
  754. ?assertMatch([{a, alpha}, {b, beta}|_], Metadata),
  755. ?assertEqual("hello", lists:flatten(Message)),
  756. {_Level, _Time2, Message2, _Metadata2} = pop(?TEST_SINK_EVENT),
  757. ?assertEqual("hello world", lists:flatten(Message2)),
  758. {_Level, _Time3, Message3, _Metadata3} = pop(?TEST_SINK_EVENT),
  759. ?assertEqual("format world", lists:flatten(Message3)),
  760. {_Level, _Time4, Message4, _Metadata4} = pop(?TEST_SINK_EVENT),
  761. ?assertEqual("hello world", lists:flatten(Message4)),
  762. {_Level, _Time5, Message5, _Metadata5} = pop(?TEST_SINK_EVENT),
  763. ?assertEqual("hello world", lists:flatten(Message5)),
  764. {_Level, _Time6, Message6, Metadata6} = pop(?TEST_SINK_EVENT),
  765. ?assertMatch([{d, delta}, {g, gamma}|_], Metadata6),
  766. ?assertEqual("format world", lists:flatten(Message6)),
  767. ok
  768. end
  769. },
  770. {"stopped trace stops and removes its event handler - test sink (gh#267)",
  771. fun() ->
  772. Sink = ?TEST_SINK_EVENT,
  773. StartHandlers = gen_event:which_handlers(Sink),
  774. {_, T0} = lager_config:get({Sink, loglevel}),
  775. StartGlobal = lager_config:global_get(handlers),
  776. ?assertEqual([], T0),
  777. {ok, TestTrace1} = lager:trace_file("/tmp/test", [{sink, Sink}, {a,b}]),
  778. MidHandlers = gen_event:which_handlers(Sink),
  779. {ok, TestTrace2} = lager:trace_file("/tmp/test", [{sink, Sink}, {c,d}]),
  780. MidHandlers = gen_event:which_handlers(Sink),
  781. ?assertEqual(length(StartHandlers)+1, length(MidHandlers)),
  782. MidGlobal = lager_config:global_get(handlers),
  783. ?assertEqual(length(StartGlobal)+1, length(MidGlobal)),
  784. {_, T1} = lager_config:get({Sink, loglevel}),
  785. ?assertEqual(2, length(T1)),
  786. ok = lager:stop_trace(TestTrace1),
  787. {_, T2} = lager_config:get({Sink, loglevel}),
  788. ?assertEqual(1, length(T2)),
  789. ?assertEqual(length(StartHandlers)+1, length(
  790. gen_event:which_handlers(Sink))),
  791. ?assertEqual(length(StartGlobal)+1, length(lager_config:global_get(handlers))),
  792. ok = lager:stop_trace(TestTrace2),
  793. EndHandlers = gen_event:which_handlers(Sink),
  794. EndGlobal = lager_config:global_get(handlers),
  795. {_, T3} = lager_config:get({Sink, loglevel}),
  796. ?assertEqual([], T3),
  797. ?assertEqual(StartHandlers, EndHandlers),
  798. ?assertEqual(StartGlobal, EndGlobal),
  799. ok
  800. end
  801. },
  802. {"log messages below the threshold are ignored",
  803. fun() ->
  804. ?assertEqual(0, count(?TEST_SINK_EVENT)),
  805. ?TEST_SINK_NAME:debug("this message will be ignored"),
  806. ?assertEqual(0, count(?TEST_SINK_EVENT)),
  807. ?assertEqual(0, count_ignored(?TEST_SINK_EVENT)),
  808. lager_config:set({?TEST_SINK_EVENT, loglevel}, {element(2, lager_util:config_to_mask(debug)), []}),
  809. ?TEST_SINK_NAME:debug("this message should be ignored"),
  810. ?assertEqual(0, count(?TEST_SINK_EVENT)),
  811. ?assertEqual(1, count_ignored(?TEST_SINK_EVENT)),
  812. lager:set_loglevel(?TEST_SINK_EVENT, ?MODULE, undefined, debug),
  813. ?assertEqual({?DEBUG bor ?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({?TEST_SINK_EVENT, loglevel})),
  814. ?TEST_SINK_NAME:debug("this message should be logged"),
  815. ?assertEqual(1, count(?TEST_SINK_EVENT)),
  816. ?assertEqual(1, count_ignored(?TEST_SINK_EVENT)),
  817. ?assertEqual(debug, lager:get_loglevel(?TEST_SINK_EVENT, ?MODULE)),
  818. ok
  819. end
  820. }
  821. ]
  822. }.
  823. setup_sink() ->
  824. error_logger:tty(false),
  825. application:load(lager),
  826. application:set_env(lager, handlers, []),
  827. application:set_env(lager, error_logger_redirect, false),
  828. application:set_env(lager, extra_sinks, [{?TEST_SINK_EVENT, [{handlers, [{?MODULE, info}]}]}]),
  829. lager:start(),
  830. gen_event:call(lager_event, ?MODULE, flush),
  831. gen_event:call(?TEST_SINK_EVENT, ?MODULE, flush).
  832. setup() ->
  833. error_logger:tty(false),
  834. application:load(lager),
  835. application:set_env(lager, handlers, [{?MODULE, info}]),
  836. application:set_env(lager, error_logger_redirect, false),
  837. application:unset_env(lager, traces),
  838. lager:start(),
  839. %% There is a race condition between the application start up, lager logging its own
  840. %% start up condition and several tests that count messages or parse the output of
  841. %% tests. When the lager start up message wins the race, it causes these tests
  842. %% which parse output or count message arrivals to fail.
  843. %%
  844. %% We introduce a sleep here to allow `flush' to arrive *after* the start up
  845. %% message has been received and processed.
  846. %%
  847. %% This race condition was first exposed during the work on
  848. %% 4b5260c4524688b545cc12da6baa2dfa4f2afec9 which introduced the lager
  849. %% manager killer PR.
  850. timer:sleep(5),
  851. gen_event:call(lager_event, ?MODULE, flush).
  852. cleanup(_) ->
  853. catch ets:delete(lager_config), %% kill the ets config table with fire
  854. application:stop(lager),
  855. application:stop(goldrush),
  856. error_logger:tty(true).
  857. crash(Type) ->
  858. spawn(fun() -> gen_server:call(crash, Type) end),
  859. timer:sleep(100),
  860. _ = gen_event:which_handlers(error_logger),
  861. ok.
  862. test_body(Expected, Actual) ->
  863. case has_line_numbers() of
  864. true ->
  865. ExLen = length(Expected),
  866. {Body, Rest} = case length(Actual) > ExLen of
  867. true ->
  868. {string:substr(Actual, 1, ExLen),
  869. string:substr(Actual, (ExLen + 1))};
  870. _ ->
  871. {Actual, []}
  872. end,
  873. ?assertEqual(Expected, Body),
  874. % OTP-17 (and maybe later releases) may tack on additional info
  875. % about the failure, so if Actual starts with Expected (already
  876. % confirmed by having gotten past assertEqual above) and ends
  877. % with " line NNN" we can ignore what's in-between. By extension,
  878. % since there may not be line information appended at all, any
  879. % text we DO find is reportable, but not a test failure.
  880. case Rest of
  881. [] ->
  882. ok;
  883. _ ->
  884. % isolate the extra data and report it if it's not just
  885. % a line number indicator
  886. case re:run(Rest, "^.*( line \\d+)$", [{capture, [1]}]) of
  887. nomatch ->
  888. ?debugFmt(
  889. "Trailing data \"~s\" following \"~s\"",
  890. [Rest, Expected]);
  891. {match, [{0, _}]} ->
  892. % the whole sting is " line NNN"
  893. ok;
  894. {match, [{Off, _}]} ->
  895. ?debugFmt(
  896. "Trailing data \"~s\" following \"~s\"",
  897. [string:substr(Rest, 1, Off), Expected])
  898. end
  899. end;
  900. _ ->
  901. ?assertEqual(Expected, Actual)
  902. end.
  903. error_logger_redirect_crash_setup() ->
  904. error_logger:tty(false),
  905. application:load(lager),
  906. application:set_env(lager, error_logger_redirect, true),
  907. application:set_env(lager, handlers, [{?MODULE, error}]),
  908. lager:start(),
  909. crash:start(),
  910. lager_event.
  911. error_logger_redirect_crash_setup_sink() ->
  912. error_logger:tty(false),
  913. application:load(lager),
  914. application:set_env(lager, error_logger_redirect, true),
  915. application:unset_env(lager, handlers),
  916. application:set_env(lager, extra_sinks, [
  917. {error_logger_lager_event, [
  918. {handlers, [{?MODULE, error}]}]}]),
  919. lager:start(),
  920. crash:start(),
  921. error_logger_lager_event.
  922. error_logger_redirect_crash_cleanup(_Sink) ->
  923. application:stop(lager),
  924. application:stop(goldrush),
  925. application:unset_env(lager, extra_sinks),
  926. case whereis(crash) of
  927. undefined -> ok;
  928. Pid -> exit(Pid, kill)
  929. end,
  930. error_logger:tty(true).
  931. crash_fsm_setup() ->
  932. error_logger:tty(false),
  933. application:load(lager),
  934. application:set_env(lager, error_logger_redirect, true),
  935. application:set_env(lager, handlers, [{?MODULE, error}]),
  936. lager:start(),
  937. crash_fsm:start(),
  938. crash_statem:start(),
  939. lager:log(error, self(), "flush flush"),
  940. timer:sleep(100),
  941. gen_event:call(lager_event, ?MODULE, flush),
  942. lager_event.
  943. crash_fsm_sink_setup() ->
  944. ErrorSink = error_logger_lager_event,
  945. error_logger:tty(false),
  946. application:load(lager),
  947. application:set_env(lager, error_logger_redirect, true),
  948. application:set_env(lager, handlers, []),
  949. application:set_env(lager, extra_sinks, [{ErrorSink, [{handlers, [{?MODULE, error}]}]}]),
  950. lager:start(),
  951. crash_fsm:start(),
  952. crash_statem:start(),
  953. lager:log(ErrorSink, error, self(), "flush flush", []),
  954. timer:sleep(100),
  955. flush(ErrorSink),
  956. ErrorSink.
  957. crash_fsm_cleanup(_Sink) ->
  958. application:stop(lager),
  959. application:stop(goldrush),
  960. application:unset_env(lager, extra_sinks),
  961. lists:foreach(fun(N) -> kill_crasher(N) end, [crash_fsm, crash_statem]),
  962. error_logger:tty(true).
  963. kill_crasher(RegName) ->
  964. case whereis(RegName) of
  965. undefined -> ok;
  966. Pid -> exit(Pid, kill)
  967. end.
  968. spawn_fsm_crash(Module) ->
  969. spawn(fun() -> Module:crash() end),
  970. timer:sleep(100),
  971. _ = gen_event:which_handlers(error_logger),
  972. ok.
  973. crash_fsm_test_() ->
  974. TestBody = fun(Name, FsmModule, Expected) ->
  975. fun(Sink) ->
  976. {Name,
  977. fun() ->
  978. case {FsmModule =:= crash_statem, lager_util:otp_version() < 19} of
  979. {true, true} -> ok;
  980. _ ->
  981. Pid = whereis(FsmModule),
  982. spawn_fsm_crash(FsmModule),
  983. {Level, _, Msg, Metadata} = pop(Sink),
  984. test_body(Expected, lists:flatten(Msg)),
  985. ?assertEqual(Pid, proplists:get_value(pid, Metadata)),
  986. ?assertEqual(lager_util:level_to_num(error), Level)
  987. end
  988. end
  989. }
  990. end
  991. end,
  992. Tests = [
  993. fun(Sink) ->
  994. {"again, there is nothing up my sleeve",
  995. fun() ->
  996. ?assertEqual(undefined, pop(Sink)),
  997. ?assertEqual(0, count(Sink))
  998. end
  999. }
  1000. end,
  1001. TestBody("gen_fsm crash", crash_fsm, "gen_fsm crash_fsm in state state1 terminated with reason: call to undefined function crash_fsm:state1/3 from gen_fsm:handle_msg/"),
  1002. TestBody("gen_statem crash", crash_statem, "gen_statem crash_statem in state state1 terminated with reason: no function clause matching crash_statem:handle")
  1003. ],
  1004. {"FSM crash output tests", [
  1005. {"Default sink",
  1006. {foreach,
  1007. fun crash_fsm_setup/0,
  1008. fun crash_fsm_cleanup/1,
  1009. Tests}},
  1010. {"Error logger sink",
  1011. {foreach,
  1012. fun crash_fsm_sink_setup/0,
  1013. fun crash_fsm_cleanup/1,
  1014. Tests}}
  1015. ]}.
  1016. error_logger_redirect_crash_test_() ->
  1017. TestBody=fun(Name,CrashReason,Expected) ->
  1018. fun(Sink) ->
  1019. {Name,
  1020. fun() ->
  1021. Pid = whereis(crash),
  1022. crash(CrashReason),
  1023. {Level, _, Msg,Metadata} = pop(Sink),
  1024. test_body(Expected, lists:flatten(Msg)),
  1025. ?assertEqual(Pid,proplists:get_value(pid,Metadata)),
  1026. ?assertEqual(lager_util:level_to_num(error),Level)
  1027. end
  1028. }
  1029. end
  1030. end,
  1031. Tests = [
  1032. fun(Sink) ->
  1033. {"again, there is nothing up my sleeve",
  1034. fun() ->
  1035. ?assertEqual(undefined, pop(Sink)),
  1036. ?assertEqual(0, count(Sink))
  1037. end
  1038. }
  1039. end,
  1040. TestBody("bad return value",bad_return,"gen_server crash terminated with reason: bad return value: bleh"),
  1041. TestBody("bad return value with string",bad_return_string,"gen_server crash terminated with reason: bad return value: {tuple,{tuple,\"string\"}}"),
  1042. TestBody("bad return uncaught throw",throw,"gen_server crash terminated with reason: bad return value: a_ball"),
  1043. TestBody("case clause",case_clause,"gen_server crash terminated with reason: no case clause matching {} in crash:handle_call/3"),
  1044. TestBody("case clause string",case_clause_string,"gen_server crash terminated with reason: no case clause matching \"crash\" in crash:handle_call/3"),
  1045. TestBody("function clause",function_clause,"gen_server crash terminated with reason: no function clause matching crash:function({})"),
  1046. TestBody("if clause",if_clause,"gen_server crash terminated with reason: no true branch found while evaluating if expression in crash:handle_call/3"),
  1047. TestBody("try clause",try_clause,"gen_server crash terminated with reason: no try clause matching [] in crash:handle_call/3"),
  1048. TestBody("undefined function",undef,"gen_server crash terminated with reason: call to undefined function crash:booger/0 from crash:handle_call/3"),
  1049. TestBody("bad math",badarith,"gen_server crash terminated with reason: bad arithmetic expression in crash:handle_call/3"),
  1050. TestBody("bad match",badmatch,"gen_server crash terminated with reason: no match of right hand value {} in crash:handle_call/3"),
  1051. TestBody("bad arity",badarity,"gen_server crash terminated with reason: fun called with wrong arity of 1 instead of 3 in crash:handle_call/3"),
  1052. TestBody("bad arg1",badarg1,"gen_server crash terminated with reason: bad argument in crash:handle_call/3"),
  1053. TestBody("bad arg2",badarg2,"gen_server crash terminated with reason: bad argument in call to erlang:iolist_to_binary([\"foo\",bar]) in crash:handle_call/3"),
  1054. TestBody("bad record",badrecord,"gen_server crash terminated with reason: bad record state in crash:handle_call/3"),
  1055. TestBody("noproc",noproc,"gen_server crash terminated with reason: no such process or port in call to gen_event:call(foo, bar, baz)"),
  1056. TestBody("badfun",badfun,"gen_server crash terminated with reason: bad function booger in crash:handle_call/3")
  1057. ],
  1058. {"Error logger redirect crash", [
  1059. {"Redirect to default sink",
  1060. {foreach,
  1061. fun error_logger_redirect_crash_setup/0,
  1062. fun error_logger_redirect_crash_cleanup/1,
  1063. Tests}},
  1064. {"Redirect to error_logger_lager_event sink",
  1065. {foreach,
  1066. fun error_logger_redirect_crash_setup_sink/0,
  1067. fun error_logger_redirect_crash_cleanup/1,
  1068. Tests}}
  1069. ]}.
  1070. error_logger_redirect_setup() ->
  1071. error_logger:tty(false),
  1072. application:load(lager),
  1073. application:set_env(lager, error_logger_redirect, true),
  1074. application:set_env(lager, handlers, [{?MODULE, info}]),
  1075. lager:start(),
  1076. lager:log(error, self(), "flush flush"),
  1077. timer:sleep(100),
  1078. gen_event:call(lager_event, ?MODULE, flush),
  1079. lager_event.
  1080. error_logger_redirect_setup_sink() ->
  1081. error_logger:tty(false),
  1082. application:load(lager),
  1083. application:set_env(lager, error_logger_redirect, true),
  1084. application:unset_env(lager, handlers),
  1085. application:set_env(lager, extra_sinks, [
  1086. {error_logger_lager_event, [
  1087. {handlers, [{?MODULE, info}]}]}]),
  1088. lager:start(),
  1089. lager:log(error_logger_lager_event, error, self(), "flush flush", []),
  1090. timer:sleep(100),
  1091. gen_event:call(error_logger_lager_event, ?MODULE, flush),
  1092. error_logger_lager_event.
  1093. error_logger_redirect_cleanup(_) ->
  1094. application:stop(lager),
  1095. application:stop(goldrush),
  1096. application:unset_env(lager, extra_sinks),
  1097. error_logger:tty(true).
  1098. error_logger_redirect_test_() ->
  1099. Tests = [
  1100. {"error reports are printed",
  1101. fun(Sink) ->
  1102. sync_error_logger:error_report([{this, is}, a, {silly, format}]),
  1103. _ = gen_event:which_handlers(error_logger),
  1104. {Level, _, Msg,Metadata} = pop(Sink),
  1105. ?assertEqual(lager_util:level_to_num(error),Level),
  1106. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1107. Expected = "this: is, a, silly: format",
  1108. ?assertEqual(Expected, lists:flatten(Msg))
  1109. end
  1110. },
  1111. {"string error reports are printed",
  1112. fun(Sink) ->
  1113. sync_error_logger:error_report("this is less silly"),
  1114. _ = gen_event:which_handlers(error_logger),
  1115. {Level, _, Msg,Metadata} = pop(Sink),
  1116. ?assertEqual(lager_util:level_to_num(error),Level),
  1117. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1118. Expected = "this is less silly",
  1119. ?assertEqual(Expected, lists:flatten(Msg))
  1120. end
  1121. },
  1122. {"error messages are printed",
  1123. fun(Sink) ->
  1124. sync_error_logger:error_msg("doom, doom has come upon you all"),
  1125. _ = gen_event:which_handlers(error_logger),
  1126. {Level, _, Msg,Metadata} = pop(Sink),
  1127. ?assertEqual(lager_util:level_to_num(error),Level),
  1128. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1129. Expected = "doom, doom has come upon you all",
  1130. ?assertEqual(Expected, lists:flatten(Msg))
  1131. end
  1132. },
  1133. {"error messages with unicode characters in Args are printed",
  1134. fun(Sink) ->
  1135. sync_error_logger:error_msg("~ts", ["Привет!"]),
  1136. _ = gen_event:which_handlers(error_logger),
  1137. {Level, _, Msg,Metadata} = pop(Sink),
  1138. ?assertEqual(lager_util:level_to_num(error),Level),
  1139. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1140. ?assertEqual("Привет!", lists:flatten(Msg))
  1141. end
  1142. },
  1143. {"error messages are truncated at 4096 characters",
  1144. fun(Sink) ->
  1145. sync_error_logger:error_msg("doom, doom has come upon you all ~p", [string:copies("doom", 10000)]),
  1146. _ = gen_event:which_handlers(error_logger),
  1147. {_, _, Msg,_Metadata} = pop(Sink),
  1148. ?assert(length(lists:flatten(Msg)) < 5100)
  1149. end
  1150. },
  1151. {"info reports are printed",
  1152. fun(Sink) ->
  1153. sync_error_logger:info_report([{this, is}, a, {silly, format}]),
  1154. _ = gen_event:which_handlers(error_logger),
  1155. {Level, _, Msg,Metadata} = pop(Sink),
  1156. ?assertEqual(lager_util:level_to_num(info),Level),
  1157. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1158. Expected = "this: is, a, silly: format",
  1159. ?assertEqual(Expected, lists:flatten(Msg))
  1160. end
  1161. },
  1162. {"info reports are truncated at 4096 characters",
  1163. fun(Sink) ->
  1164. sync_error_logger:info_report([[{this, is}, a, {silly, format}] || _ <- lists:seq(0, 600)]),
  1165. _ = gen_event:which_handlers(error_logger),
  1166. {Level, _, Msg,Metadata} = pop(Sink),
  1167. ?assertEqual(lager_util:level_to_num(info),Level),
  1168. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1169. ?assert(length(lists:flatten(Msg)) < 5000)
  1170. end
  1171. },
  1172. {"single term info reports are printed",
  1173. fun(Sink) ->
  1174. sync_error_logger:info_report({foolish, bees}),
  1175. _ = gen_event:which_handlers(error_logger),
  1176. {Level, _, Msg,Metadata} = pop(Sink),
  1177. ?assertEqual(lager_util:level_to_num(info),Level),
  1178. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1179. ?assertEqual("{foolish,bees}", lists:flatten(Msg))
  1180. end
  1181. },
  1182. {"single term error reports are printed",
  1183. fun(Sink) ->
  1184. sync_error_logger:error_report({foolish, bees}),
  1185. _ = gen_event:which_handlers(error_logger),
  1186. {Level, _, Msg,Metadata} = pop(Sink),
  1187. ?assertEqual(lager_util:level_to_num(error),Level),
  1188. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1189. ?assertEqual("{foolish,bees}", lists:flatten(Msg))
  1190. end
  1191. },
  1192. {"string info reports are printed",
  1193. fun(Sink) ->
  1194. sync_error_logger:info_report("this is less silly"),
  1195. _ = gen_event:which_handlers(error_logger),
  1196. {Level, _, Msg,Metadata} = pop(Sink),
  1197. ?assertEqual(lager_util:level_to_num(info),Level),
  1198. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1199. ?assertEqual("this is less silly", lists:flatten(Msg))
  1200. end
  1201. },
  1202. {"string info reports are truncated at 4096 characters",
  1203. fun(Sink) ->
  1204. sync_error_logger:info_report(string:copies("this is less silly", 1000)),
  1205. _ = gen_event:which_handlers(error_logger),
  1206. {Level, _, Msg,Metadata} = pop(Sink),
  1207. ?assertEqual(lager_util:level_to_num(info),Level),
  1208. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1209. ?assert(length(lists:flatten(Msg)) < 5100)
  1210. end
  1211. },
  1212. {"strings in a mixed report are printed as strings",
  1213. fun(Sink) ->
  1214. sync_error_logger:info_report(["this is less silly", {than, "this"}]),
  1215. _ = gen_event:which_handlers(error_logger),
  1216. {Level, _, Msg,Metadata} = pop(Sink),
  1217. ?assertEqual(lager_util:level_to_num(info),Level),
  1218. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1219. ?assertEqual("\"this is less silly\", than: \"this\"", lists:flatten(Msg))
  1220. end
  1221. },
  1222. {"info messages are printed",
  1223. fun(Sink) ->
  1224. sync_error_logger:info_msg("doom, doom has come upon you all"),
  1225. _ = gen_event:which_handlers(error_logger),
  1226. {Level, _, Msg,Metadata} = pop(Sink),
  1227. ?assertEqual(lager_util:level_to_num(info),Level),
  1228. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1229. ?assertEqual("doom, doom has come upon you all", lists:flatten(Msg))
  1230. end
  1231. },
  1232. {"info messages are truncated at 4096 characters",
  1233. fun(Sink) ->
  1234. sync_error_logger:info_msg("doom, doom has come upon you all ~p", [string:copies("doom", 10000)]),
  1235. _ = gen_event:which_handlers(error_logger),
  1236. {Level, _, Msg,Metadata} = pop(Sink),
  1237. ?assertEqual(lager_util:level_to_num(info),Level),
  1238. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1239. ?assert(length(lists:flatten(Msg)) < 5100)
  1240. end
  1241. },
  1242. {"info messages with unicode characters in Args are printed",
  1243. fun(Sink) ->
  1244. sync_error_logger:info_msg("~ts", ["Привет!"]),
  1245. _ = gen_event:which_handlers(error_logger),
  1246. {Level, _, Msg,Metadata} = pop(Sink),
  1247. ?assertEqual(lager_util:level_to_num(info),Level),
  1248. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1249. ?assertEqual("Привет!", lists:flatten(Msg))
  1250. end
  1251. },
  1252. {"warning messages with unicode characters in Args are printed",
  1253. %% The next 4 tests need to store the current value of
  1254. %% `error_logger:warning_map/0' into a process dictionary
  1255. %% key `warning_map' so that the error message level used
  1256. %% to process the log messages will match what lager
  1257. %% expects.
  1258. %%
  1259. %% The atom returned by `error_logger:warning_map/0'
  1260. %% changed between OTP 17 and 18 (and later releases)
  1261. %%
  1262. %% `warning_map' is consumed in the `test/sync_error_logger.erl'
  1263. %% module. The default message level used in sync_error_logger
  1264. %% was fine for OTP releases through 17 and then broke
  1265. %% when 18 was released. By storing the expected value
  1266. %% in the process dictionary, sync_error_logger will
  1267. %% use the correct message level to process the
  1268. %% messages and these tests will no longer
  1269. %% break.
  1270. fun(Sink) ->
  1271. Lvl = error_logger:warning_map(),
  1272. put(warning_map, Lvl),
  1273. sync_error_logger:warning_msg("~ts", ["Привет!"]),
  1274. _ = gen_event:which_handlers(error_logger),
  1275. {Level, _, Msg,Metadata} = pop(Sink),
  1276. ?assertEqual(lager_util:level_to_num(Lvl),Level),
  1277. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1278. ?assertEqual("Привет!", lists:flatten(Msg))
  1279. end
  1280. },
  1281. {"warning messages are printed at the correct level",
  1282. fun(Sink) ->
  1283. Lvl = error_logger:warning_map(),
  1284. put(warning_map, Lvl),
  1285. sync_error_logger:warning_msg("doom, doom has come upon you all"),
  1286. _ = gen_event:which_handlers(error_logger),
  1287. {Level, _, Msg,Metadata} = pop(Sink),
  1288. ?assertEqual(lager_util:level_to_num(Lvl),Level),
  1289. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1290. ?assertEqual("doom, doom has come upon you all", lists:flatten(Msg))
  1291. end
  1292. },
  1293. {"warning reports are printed at the correct level",
  1294. fun(Sink) ->
  1295. Lvl = error_logger:warning_map(),
  1296. put(warning_map, Lvl),
  1297. sync_error_logger:warning_report([{i, like}, pie]),
  1298. _ = gen_event:which_handlers(error_logger),
  1299. {Level, _, Msg,Metadata} = pop(Sink),
  1300. ?assertEqual(lager_util:level_to_num(Lvl),Level),
  1301. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1302. ?assertEqual("i: like, pie", lists:flatten(Msg))
  1303. end
  1304. },
  1305. {"single term warning reports are printed at the correct level",
  1306. fun(Sink) ->
  1307. Lvl = error_logger:warning_map(),
  1308. put(warning_map, Lvl),
  1309. sync_error_logger:warning_report({foolish, bees}),
  1310. _ = gen_event:which_handlers(error_logger),
  1311. {Level, _, Msg,Metadata} = pop(Sink),
  1312. ?assertEqual(lager_util:level_to_num(Lvl),Level),
  1313. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1314. ?assertEqual("{foolish,bees}", lists:flatten(Msg))
  1315. end
  1316. },
  1317. {"application stop reports",
  1318. fun(Sink) ->
  1319. sync_error_logger:info_report([{application, foo}, {exited, quittin_time}, {type, lazy}]),
  1320. _ = gen_event:which_handlers(error_logger),
  1321. {Level, _, Msg,Metadata} = pop(Sink),
  1322. ?assertEqual(lager_util:level_to_num(info),Level),
  1323. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1324. ?assertEqual("Application foo exited with reason: quittin_time", lists:flatten(Msg))
  1325. end
  1326. },
  1327. {"supervisor reports",
  1328. fun(Sink) ->
  1329. sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{name, mini_steve}, {mfargs, {a, b, [c]}}, {pid, bleh}]}, {reason, fired}, {supervisor, {local, steve}}]),
  1330. _ = gen_event:which_handlers(error_logger),
  1331. {Level, _, Msg,Metadata} = pop(Sink),
  1332. ?assertEqual(lager_util:level_to_num(error),Level),
  1333. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1334. ?assertEqual("Supervisor steve had child mini_steve started with a:b(c) at bleh exit with reason fired in context france", lists:flatten(Msg))
  1335. end
  1336. },
  1337. {"supervisor reports with real error",
  1338. fun(Sink) ->
  1339. sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{name, mini_steve}, {mfargs, {a, b, [c]}}, {pid, bleh}]}, {reason, {function_clause,[{crash,handle_info,[foo]}]}}, {supervisor, {local, steve}}]),
  1340. _ = gen_event:which_handlers(error_logger),
  1341. {Level, _, Msg,Metadata} = pop(Sink),
  1342. ?assertEqual(lager_util:level_to_num(error),Level),
  1343. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1344. ?assertEqual("Supervisor steve had child mini_steve started with a:b(c) at bleh exit with reason no function clause matching crash:handle_info(foo) in context france", lists:flatten(Msg))
  1345. end
  1346. },
  1347. {"supervisor reports with real error and pid",
  1348. fun(Sink) ->
  1349. sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{name, mini_steve}, {mfargs, {a, b, [c]}}, {pid, bleh}]}, {reason, {function_clause,[{crash,handle_info,[foo]}]}}, {supervisor, somepid}]),
  1350. _ = gen_event:which_handlers(error_logger),
  1351. {Level, _, Msg,Metadata} = pop(Sink),
  1352. ?assertEqual(lager_util:level_to_num(error),Level),
  1353. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1354. ?assertEqual("Supervisor somepid had child mini_steve started with a:b(c) at bleh exit with reason no function clause matching crash:handle_info(foo) in context france", lists:flatten(Msg))
  1355. end
  1356. },
  1357. {"supervisor_bridge reports",
  1358. fun(Sink) ->
  1359. sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{mod, mini_steve}, {pid, bleh}]}, {reason, fired}, {supervisor, {local, steve}}]),
  1360. _ = gen_event:which_handlers(error_logger),
  1361. {Level, _, Msg,Metadata} = pop(Sink),
  1362. ?assertEqual(lager_util:level_to_num(error),Level),
  1363. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1364. ?assertEqual("Supervisor steve had child at module mini_steve at bleh exit with reason fired in context france", lists:flatten(Msg))
  1365. end
  1366. },
  1367. {"application progress report",
  1368. fun(Sink) ->
  1369. sync_error_logger:info_report(progress, [{application, foo}, {started_at, node()}]),
  1370. _ = gen_event:which_handlers(error_logger),
  1371. {Level, _, Msg,Metadata} = pop(Sink),
  1372. ?assertEqual(lager_util:level_to_num(info),Level),
  1373. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1374. Expected = lists:flatten(io_lib:format("Application foo started on node ~w", [node()])),
  1375. ?assertEqual(Expected, lists:flatten(Msg))
  1376. end
  1377. },
  1378. {"supervisor progress report",
  1379. fun(Sink) ->
  1380. lager:set_loglevel(Sink, ?MODULE, undefined, debug),
  1381. ?assertEqual({?DEBUG bor ?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({Sink, loglevel})),
  1382. sync_error_logger:info_report(progress, [{supervisor, {local, foo}}, {started, [{mfargs, {foo, bar, 1}}, {pid, baz}]}]),
  1383. _ = gen_event:which_handlers(error_logger),
  1384. {Level, _, Msg,Metadata} = pop(Sink),
  1385. ?assertEqual(lager_util:level_to_num(debug),Level),
  1386. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1387. ?assertEqual("Supervisor foo started foo:bar/1 at pid baz", lists:flatten(Msg))
  1388. end
  1389. },
  1390. {"supervisor progress report with pid",
  1391. fun(Sink) ->
  1392. lager:set_loglevel(Sink, ?MODULE, undefined, debug),
  1393. ?assertEqual({?DEBUG bor ?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({Sink, loglevel})),
  1394. sync_error_logger:info_report(progress, [{supervisor, somepid}, {started, [{mfargs, {foo, bar, 1}}, {pid, baz}]}]),
  1395. _ = gen_event:which_handlers(error_logger),
  1396. {Level, _, Msg,Metadata} = pop(Sink),
  1397. ?assertEqual(lager_util:level_to_num(debug),Level),
  1398. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1399. ?assertEqual("Supervisor somepid started foo:bar/1 at pid baz", lists:flatten(Msg))
  1400. end
  1401. },
  1402. {"crash report for emfile",
  1403. fun(Sink) ->
  1404. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, emfile, [{stack, trace, 1}]}}], []]),
  1405. _ = gen_event:which_handlers(error_logger),
  1406. {Level, _, Msg,Metadata} = pop(Sink),
  1407. ?assertEqual(lager_util:level_to_num(error),Level),
  1408. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1409. Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: maximum number of file descriptors exhausted, check ulimit -n", [self()])),
  1410. ?assertEqual(Expected, lists:flatten(Msg))
  1411. end
  1412. },
  1413. {"crash report for system process limit",
  1414. fun(Sink) ->
  1415. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, system_limit, [{erlang, spawn, 1}]}}], []]),
  1416. _ = gen_event:which_handlers(error_logger),
  1417. {Level, _, Msg,Metadata} = pop(Sink),
  1418. ?assertEqual(lager_util:level_to_num(error),Level),
  1419. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1420. Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of processes exceeded", [self()])),
  1421. ?assertEqual(Expected, lists:flatten(Msg))
  1422. end
  1423. },
  1424. {"crash report for system process limit2",
  1425. fun(Sink) ->
  1426. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, system_limit, [{erlang, spawn_opt, 1}]}}], []]),
  1427. _ = gen_event:which_handlers(error_logger),
  1428. {Level, _, Msg,Metadata} = pop(Sink),
  1429. ?assertEqual(lager_util:level_to_num(error),Level),
  1430. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1431. Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of processes exceeded", [self()])),
  1432. ?assertEqual(Expected, lists:flatten(Msg))
  1433. end
  1434. },
  1435. {"crash report for system port limit",
  1436. fun(Sink) ->
  1437. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, system_limit, [{erlang, open_port, 1}]}}], []]),
  1438. _ = gen_event:which_handlers(error_logger),
  1439. {Level, _, Msg,Metadata} = pop(Sink),
  1440. ?assertEqual(lager_util:level_to_num(error),Level),
  1441. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1442. Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of ports exceeded", [self()])),
  1443. ?assertEqual(Expected, lists:flatten(Msg))
  1444. end
  1445. },
  1446. {"crash report for system port limit",
  1447. fun(Sink) ->
  1448. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, system_limit, [{erlang, list_to_atom, 1}]}}], []]),
  1449. _ = gen_event:which_handlers(error_logger),
  1450. {Level, _, Msg,Metadata} = pop(Sink),
  1451. ?assertEqual(lager_util:level_to_num(error),Level),
  1452. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1453. Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: tried to create an atom larger than 255, or maximum atom count exceeded", [self()])),
  1454. ?assertEqual(Expected, lists:flatten(Msg))
  1455. end
  1456. },
  1457. {"crash report for system ets table limit",
  1458. fun(Sink) ->
  1459. sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, test}, {error_info, {error, system_limit, [{ets,new,[segment_offsets,[ordered_set,public]]},{mi_segment,open_write,1},{mi_buffer_converter,handle_cast,2},{gen_server,handle_msg,5},{proc_lib,init_p_do_apply,3}]}}], []]),
  1460. _ = gen_event:which_handlers(error_logger),
  1461. {Level, _, Msg,Metadata} = pop(Sink),
  1462. ?assertEqual(lager_util:level_to_num(error),Level),
  1463. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1464. Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of ETS tables exceeded", [test])),
  1465. ?assertEqual(Expected, lists:flatten(Msg))
  1466. end
  1467. },
  1468. {"crash report for unknown system limit should be truncated at 500 characters",
  1469. fun(Sink) ->
  1470. sync_error_logger:error_report(crash_report, [[{pid, self()}, {error_info, {error, system_limit, [{wtf,boom,[string:copies("aaaa", 4096)]}]}}], []]),
  1471. _ = gen_event:which_handlers(error_logger),
  1472. {_, _, Msg,_Metadata} = pop(Sink),
  1473. ?assert(length(lists:flatten(Msg)) > 550),
  1474. ?assert(length(lists:flatten(Msg)) < 600)
  1475. end
  1476. },
  1477. {"crash reports for 'special processes' should be handled right - function_clause",
  1478. fun(Sink) ->
  1479. {ok, Pid} = special_process:start(),
  1480. unlink(Pid),
  1481. Pid ! function_clause,
  1482. timer:sleep(500),
  1483. _ = gen_event:which_handlers(error_logger),
  1484. {_, _, Msg, _Metadata} = pop(Sink),
  1485. Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~p with 0 neighbours crashed with reason: no function clause matching special_process:foo(bar)",
  1486. [Pid])),
  1487. test_body(Expected, lists:flatten(Msg))
  1488. end
  1489. },
  1490. {"crash reports for 'special processes' should be handled right - case_clause",
  1491. fun(Sink) ->
  1492. {ok, Pid} = special_process:start(),
  1493. unlink(Pid),
  1494. Pid ! {case_clause, wtf},
  1495. timer:sleep(500),
  1496. _ = gen_event:which_handlers(error_logger),
  1497. {_, _, Msg, _Metadata} = pop(Sink),
  1498. Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~p with 0 neighbours crashed with reason: no case clause matching wtf in special_process:loop/0",
  1499. [Pid])),
  1500. test_body(Expected, lists:flatten(Msg))
  1501. end
  1502. },
  1503. {"crash reports for 'special processes' should be handled right - exit",
  1504. fun(Sink) ->
  1505. {ok, Pid} = special_process:start(),
  1506. unlink(Pid),
  1507. Pid ! exit,
  1508. timer:sleep(500),
  1509. _ = gen_event:which_handlers(error_logger),
  1510. {_, _, Msg, _Metadata} = pop(Sink),
  1511. Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~p with 0 neighbours exited with reason: byebye in special_process:loop/0",
  1512. [Pid])),
  1513. test_body(Expected, lists:flatten(Msg))
  1514. end
  1515. },
  1516. {"crash reports for 'special processes' should be handled right - error",
  1517. fun(Sink) ->
  1518. {ok, Pid} = special_process:start(),
  1519. unlink(Pid),
  1520. Pid ! error,
  1521. timer:sleep(500),
  1522. _ = gen_event:which_handlers(error_logger),
  1523. {_, _, Msg, _Metadata} = pop(Sink),
  1524. Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~p with 0 neighbours crashed with reason: mybad in special_process:loop/0",
  1525. [Pid])),
  1526. test_body(Expected, lists:flatten(Msg))
  1527. end
  1528. },
  1529. {"webmachine error reports",
  1530. fun(Sink) ->
  1531. Path = "/cgi-bin/phpmyadmin",
  1532. Reason = {error,{error,{badmatch,{error,timeout}},
  1533. [{myapp,dostuff,2,[{file,"src/myapp.erl"},{line,123}]},
  1534. {webmachine_resource,resource_call,3,[{file,"src/webmachine_resource.erl"},{line,169}]}]}},
  1535. sync_error_logger:error_msg("webmachine error: path=~p~n~p~n", [Path, Reason]),
  1536. _ = gen_event:which_handlers(error_logger),
  1537. {Level, _, Msg,Metadata} = pop(Sink),
  1538. ?assertEqual(lager_util:level_to_num(error),Level),
  1539. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1540. ?assertEqual("Webmachine error at path \"/cgi-bin/phpmyadmin\" : no match of right hand value {error,timeout} in myapp:dostuff/2 line 123", lists:flatten(Msg))
  1541. end
  1542. },
  1543. {"Cowboy error reports, 8 arg version",
  1544. fun(Sink) ->
  1545. Stack = [{my_handler,init, 3,[{file,"src/my_handler.erl"},{line,123}]},
  1546. {cowboy_handler,handler_init,4,[{file,"src/cowboy_handler.erl"},{line,169}]}],
  1547. sync_error_logger:error_msg(
  1548. "** Cowboy handler ~p terminating in ~p/~p~n"
  1549. " for the reason ~p:~p~n"
  1550. "** Options were ~p~n"
  1551. "** Request was ~p~n"
  1552. "** Stacktrace: ~p~n~n",
  1553. [my_handler, init, 3, error, {badmatch, {error, timeout}}, [],
  1554. "Request", Stack]),
  1555. _ = gen_event:which_handlers(error_logger),
  1556. {Level, _, Msg,Metadata} = pop(Sink),
  1557. ?assertEqual(lager_util:level_to_num(error),Level),
  1558. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1559. ?assertEqual("Cowboy handler my_handler terminated in my_handler:init/3 with reason: no match of right hand value {error,timeout} in my_handler:init/3 line 123", lists:flatten(Msg))
  1560. end
  1561. },
  1562. {"Cowboy error reports, 10 arg version",
  1563. fun(Sink) ->
  1564. Stack = [{my_handler,somecallback, 3,[{file,"src/my_handler.erl"},{line,123}]},
  1565. {cowboy_handler,handler_init,4,[{file,"src/cowboy_handler.erl"},{line,169}]}],
  1566. sync_error_logger:error_msg(
  1567. "** Cowboy handler ~p terminating in ~p/~p~n"
  1568. " for the reason ~p:~p~n** Message was ~p~n"
  1569. "** Options were ~p~n** Handler state was ~p~n"
  1570. "** Request was ~p~n** Stacktrace: ~p~n~n",
  1571. [my_handler, somecallback, 3, error, {badmatch, {error, timeout}}, hello, [],
  1572. {}, "Request", Stack]),
  1573. _ = gen_event:which_handlers(error_logger),
  1574. {Level, _, Msg,Metadata} = pop(Sink),
  1575. ?assertEqual(lager_util:level_to_num(error),Level),
  1576. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1577. ?assertEqual("Cowboy handler my_handler terminated in my_handler:somecallback/3 with reason: no match of right hand value {error,timeout} in my_handler:somecallback/3 line 123", lists:flatten(Msg))
  1578. end
  1579. },
  1580. {"Cowboy error reports, 5 arg version",
  1581. fun(Sink) ->
  1582. sync_error_logger:error_msg(
  1583. "** Cowboy handler ~p terminating; "
  1584. "function ~p/~p was not exported~n"
  1585. "** Request was ~p~n** State was ~p~n~n",
  1586. [my_handler, to_json, 2, "Request", {}]),
  1587. _ = gen_event:which_handlers(error_logger),
  1588. {Level, _, Msg,Metadata} = pop(Sink),
  1589. ?assertEqual(lager_util:level_to_num(error),Level),
  1590. ?assertEqual(self(),proplists:get_value(pid,Metadata)),
  1591. ?assertEqual("Cowboy handler my_handler terminated with reason: call to undefined function my_handler:to_json/2", lists:flatten(Msg))
  1592. end
  1593. },
  1594. {"messages should not be generated if they don't satisfy the threshold",
  1595. fun(Sink) ->
  1596. lager:set_loglevel(Sink, ?MODULE, undefined, error),
  1597. ?assertEqual({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({Sink, loglevel})),
  1598. sync_error_logger:info_report([hello, world]),
  1599. _ = gen_event:which_handlers(error_logger),
  1600. ?assertEqual(0, count(Sink)),
  1601. ?assertEqual(0, count_ignored(Sink)),
  1602. lager:set_loglevel(Sink, ?MODULE, undefined, info),
  1603. ?assertEqual({?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({Sink, loglevel})),
  1604. sync_error_logger:info_report([hello, world]),
  1605. _ = gen_event:which_handlers(error_logger),
  1606. ?assertEqual(1, count(Sink)),
  1607. ?assertEqual(0, count_ignored(Sink)),
  1608. lager:set_loglevel(Sink, ?MODULE, undefined, error),
  1609. ?assertEqual({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({Sink, loglevel})),
  1610. lager_config:set({Sink, loglevel}, {element(2, lager_util:config_to_mask(debug)), []}),
  1611. sync_error_logger:info_report([hello, world]),
  1612. _ = gen_event:which_handlers(error_logger),
  1613. ?assertEqual(1, count(Sink)),
  1614. ?assertEqual(1, count_ignored(Sink))
  1615. end
  1616. }
  1617. ],
  1618. SinkTests = lists:map(
  1619. fun({Name, F}) ->
  1620. fun(Sink) -> {Name, fun() -> F(Sink) end} end
  1621. end,
  1622. Tests),
  1623. {"Error logger redirect", [
  1624. {"Redirect to default sink",
  1625. {foreach,
  1626. fun error_logger_redirect_setup/0,
  1627. fun error_logger_redirect_cleanup/1,
  1628. SinkTests}},
  1629. {"Redirect to error_logger_lager_event sink",
  1630. {foreach,
  1631. fun error_logger_redirect_setup_sink/0,
  1632. fun error_logger_redirect_cleanup/1,
  1633. SinkTests}}
  1634. ]}.
  1635. safe_format_test() ->
  1636. ?assertEqual("foo bar", lists:flatten(lager:safe_format("~p ~p", [foo, bar], 1024))),
  1637. ?assertEqual("FORMAT ERROR: \"~p ~p ~p\" [foo,bar]", lists:flatten(lager:safe_format("~p ~p ~p", [foo, bar], 1024))),
  1638. ok.
  1639. unsafe_format_test() ->
  1640. ?assertEqual("foo bar", lists:flatten(lager:unsafe_format("~p ~p", [foo, bar]))),
  1641. ?assertEqual("FORMAT ERROR: \"~p ~p ~p\" [foo,bar]", lists:flatten(lager:unsafe_format("~p ~p ~p", [foo, bar]))),
  1642. ok.
  1643. async_threshold_test_() ->
  1644. Cleanup = fun(Reset) ->
  1645. _ = error_logger:tty(false),
  1646. _ = application:stop(lager),
  1647. _ = application:stop(goldrush),
  1648. _ = application:unset_env(lager, async_threshold),
  1649. if
  1650. Reset ->
  1651. true = ets:delete(async_threshold_test),
  1652. error_logger:tty(true);
  1653. true ->
  1654. _ = (catch ets:delete(async_threshold_test)),
  1655. ok
  1656. end
  1657. end,
  1658. Setup = fun() ->
  1659. % Evidence suggests that previous tests somewhere are leaving some of this stuff
  1660. % loaded, and cleaning it out forcefully to allows the test to succeed.
  1661. _ = Cleanup(false),
  1662. _ = ets:new(async_threshold_test, [set, named_table, public]),
  1663. ?assertEqual(true, ets:insert_new(async_threshold_test, {sync_toggled, 0})),
  1664. ?assertEqual(true, ets:insert_new(async_threshold_test, {async_toggled, 0})),
  1665. _ = application:load(lager),
  1666. ok = application:set_env(lager, error_logger_redirect, false),
  1667. ok = application:set_env(lager, async_threshold, 2),
  1668. ok = application:set_env(lager, async_threshold_window, 1),
  1669. ok = application:set_env(lager, handlers, [{?MODULE, info}]),
  1670. ok = lager:start(),
  1671. true
  1672. end,
  1673. {foreach, Setup, Cleanup, [
  1674. {"async threshold works",
  1675. {timeout, 30, fun() ->
  1676. %% we start out async
  1677. ?assertEqual(true, lager_config:get(async)),
  1678. ?assertEqual([{sync_toggled, 0}],
  1679. ets:lookup(async_threshold_test, sync_toggled)),
  1680. %% put a ton of things in the queue
  1681. WorkCnt = erlang:max(10, (erlang:system_info(schedulers) * 2)),
  1682. OtpVsn = lager_util:otp_version(),
  1683. % newer OTPs *may* handle the messages faster, so we'll send more
  1684. MsgCnt = ((OtpVsn * OtpVsn) div 2),
  1685. Workers = spawn_stuffers(WorkCnt, [MsgCnt, info, "hello world"], []),
  1686. %% serialize on mailbox
  1687. _ = gen_event:which_handlers(lager_event),
  1688. timer:sleep(500),
  1689. %% By now the flood of messages should have forced the backend throttle
  1690. %% to turn off async mode, but it's possible all outstanding requests
  1691. %% have been processed, so checking the current status (sync or async)
  1692. %% is an exercise in race control.
  1693. %% Instead, we'll see whether the backend throttle has toggled into sync
  1694. %% mode at any point in the past.
  1695. ?assertMatch([{sync_toggled, N}] when N > 0,
  1696. ets:lookup(async_threshold_test, sync_toggled)),
  1697. %% Wait for all the workers to return, meaning that all the messages have
  1698. %% been logged (since we're definitely in sync mode at the end of the run).
  1699. collect_workers(Workers),
  1700. %% serialize on the mailbox again
  1701. _ = gen_event:which_handlers(lager_event),
  1702. %% just in case...
  1703. timer:sleep(1000),
  1704. lager:info("hello world"),
  1705. _ = gen_event:which_handlers(lager_event),
  1706. %% async is true again now that the mailbox has drained
  1707. ?assertEqual(true, lager_config:get(async)),
  1708. ok
  1709. end}}
  1710. ]}.
  1711. % Fire off the stuffers with minimal resource overhead - speed is of the essence.
  1712. spawn_stuffers(0, _, Refs) ->
  1713. % Attempt to return them in about the order that they'll finish.
  1714. lists:reverse(Refs);
  1715. spawn_stuffers(N, Args, Refs) ->
  1716. {_Pid, Ref} = erlang:spawn_monitor(?MODULE, message_stuffer, Args),
  1717. spawn_stuffers((N - 1), Args, [Ref | Refs]).
  1718. % Spawned process to stuff N copies of Message into lager's message queue as fast as possible.
  1719. % Skip using a list function for speed and low memory footprint - don't want to take the
  1720. % resources to create a sequence (or pass one in).
  1721. message_stuffer(N, Level, Message) ->
  1722. message_stuffer_(N, Level, [{pid, erlang:self()}], Message).
  1723. message_stuffer_(0, _, _, _) ->
  1724. ok;
  1725. message_stuffer_(N, Level, Meta, Message) ->
  1726. lager:log(Level, Meta, Message),
  1727. message_stuffer_((N - 1), Level, Meta, Message).
  1728. collect_workers([]) ->
  1729. ok;
  1730. collect_workers([Ref | Refs]) ->
  1731. receive
  1732. {'DOWN', Ref, _, _, _} ->
  1733. collect_workers(Refs)
  1734. end.
  1735. produce_n_error_logger_msgs(N) ->
  1736. lists:foreach(fun (K) ->
  1737. error_logger:error_msg("Foo ~p!", [K])
  1738. end,
  1739. lists:seq(0, N-1)
  1740. ).
  1741. high_watermark_test_() ->
  1742. {foreach,
  1743. fun() ->
  1744. error_logger:tty(false),
  1745. application:load(lager),
  1746. application:set_env(lager, error_logger_redirect, true),
  1747. application:set_env(lager, handlers, [{lager_test_backend, info}]),
  1748. application:set_env(lager, async_threshold, undefined),
  1749. lager:start()
  1750. end,
  1751. fun(_) ->
  1752. application:stop(lager),
  1753. error_logger:tty(true)
  1754. end,
  1755. [
  1756. {"Nothing dropped when error_logger high watermark is undefined",
  1757. fun () ->
  1758. ok = error_logger_lager_h:set_high_water(undefined),
  1759. timer:sleep(100),
  1760. produce_n_error_logger_msgs(10),
  1761. timer:sleep(500),
  1762. ?assert(count() >= 10)
  1763. end
  1764. },
  1765. {"Mostly dropped according to error_logger high watermark",
  1766. fun () ->
  1767. ok = error_logger_lager_h:set_high_water(5),
  1768. timer:sleep(100),
  1769. produce_n_error_logger_msgs(50),
  1770. timer:sleep(1000),
  1771. ?assert(count() < 20)
  1772. end
  1773. },
  1774. {"Non-notifications are not dropped",
  1775. fun () ->
  1776. ok = error_logger_lager_h:set_high_water(2),
  1777. timer:sleep(100),
  1778. spawn(fun () -> produce_n_error_logger_msgs(300) end),
  1779. timer:sleep(50),
  1780. %% if everything were dropped, this call would be dropped
  1781. %% too, so lets hope it's not
  1782. ?assert(is_integer(count())),
  1783. timer:sleep(1000),
  1784. ?assert(count() < 10)
  1785. end
  1786. }
  1787. ]
  1788. }.
  1789. -endif.