Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1883 řádky
97 KiB

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