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.

153 regels
5.6 KiB

14 jaren geleden
13 jaren geleden
  1. %% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved.
  2. %%
  3. %% This file is provided to you under the Apache License,
  4. %% Version 2.0 (the "License"); you may not use this file
  5. %% except in compliance with the License. You may obtain
  6. %% a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing,
  11. %% software distributed under the License is distributed on an
  12. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. %% KIND, either express or implied. See the License for the
  14. %% specific language governing permissions and limitations
  15. %% under the License.
  16. %% @doc Lager's application module. Not a lot to see here.
  17. %% @private
  18. -module(lager_app).
  19. -behaviour(application).
  20. -include("lager.hrl").
  21. -ifdef(TEST).
  22. -include_lib("eunit/include/eunit.hrl").
  23. -endif.
  24. -export([start/0,
  25. start/2,
  26. stop/1]).
  27. start() ->
  28. application:start(lager).
  29. start(_StartType, _StartArgs) ->
  30. {ok, Pid} = lager_sup:start_link(),
  31. case application:get_env(lager, async_threshold) of
  32. undefined ->
  33. ok;
  34. {ok, Threshold} when is_integer(Threshold), Threshold >= 0 ->
  35. _ = supervisor:start_child(lager_handler_watcher_sup, [lager_event, lager_backend_throttle, Threshold]);
  36. {ok, BadThreshold} ->
  37. error_logger:error_msg("Invalid value for 'async_threshold': ~p~n", [BadThreshold]),
  38. throw({error, bad_config})
  39. end,
  40. Handlers = case application:get_env(lager, handlers) of
  41. undefined ->
  42. [{lager_console_backend, info},
  43. {lager_file_backend, [{"log/error.log", error, 10485760, "", 5},
  44. {"log/console.log", info, 10485760, "", 5}]}];
  45. {ok, Val} ->
  46. Val
  47. end,
  48. %% handlers failing to start are handled in the handler_watcher
  49. _ = [supervisor:start_child(lager_handler_watcher_sup, [lager_event, Module, Config]) ||
  50. {Module, Config} <- expand_handlers(Handlers)],
  51. %% mask the messages we have no use for
  52. lager:update_loglevel_config(),
  53. HighWaterMark = case application:get_env(lager, error_logger_hwm) of
  54. {ok, HwmVal} when is_integer(HwmVal), HwmVal > 0 ->
  55. HwmVal;
  56. {ok, BadVal} ->
  57. _ = lager:log(warning, self(), "Invalid error_logger high water mark: ~p, disabling", [BadVal]),
  58. undefined;
  59. undefined ->
  60. undefined
  61. end,
  62. SavedHandlers =
  63. case application:get_env(lager, error_logger_redirect) of
  64. {ok, false} ->
  65. [];
  66. _ ->
  67. case application:get_env(lager, error_logger_whitelist) of
  68. undefined ->
  69. WhiteList = [];
  70. {ok, WhiteList} ->
  71. WhiteList
  72. end,
  73. case supervisor:start_child(lager_handler_watcher_sup, [error_logger, error_logger_lager_h, [HighWaterMark]]) of
  74. {ok, _} ->
  75. [begin error_logger:delete_report_handler(X), X end ||
  76. X <- gen_event:which_handlers(error_logger) -- [error_logger_lager_h | WhiteList]];
  77. {error, _} ->
  78. []
  79. end
  80. end,
  81. {ok, Pid, SavedHandlers}.
  82. stop(Handlers) ->
  83. lists:foreach(fun(Handler) ->
  84. error_logger:add_report_handler(Handler)
  85. end, Handlers).
  86. expand_handlers([]) ->
  87. [];
  88. expand_handlers([{lager_file_backend, Configs}|T]) ->
  89. [ {lager_file_backend:config_to_id(Config), Config} || Config <- Configs] ++
  90. expand_handlers(T);
  91. expand_handlers([{Mod, Config}|T]) when is_atom(Mod) ->
  92. %% Allow the backend to generate a gen_event handler id, if it wants to.
  93. %% We don't use erlang:function_exported here because that requires the module
  94. %% already be loaded, which is unlikely at this phase of startup. Using code:load
  95. %% caused undesireable side-effects with generating code-coverage reports.
  96. Res = try Mod:config_to_id(Config) of
  97. Id ->
  98. {Id, Config}
  99. catch
  100. _:_ ->
  101. {Mod, Config}
  102. end,
  103. [Res | expand_handlers(T)];
  104. expand_handlers([H|T]) ->
  105. [H | expand_handlers(T)].
  106. -ifdef(TEST).
  107. application_config_mangling_test_() ->
  108. [{"Explode the file backend handlers",
  109. ?_assertMatch(
  110. [{lager_console_backend, info},
  111. {{lager_file_backend,"error.log"},{"error.log",error,10485760, "$D0",5}},
  112. {{lager_file_backend,"console.log"},{"console.log",info,10485760, "$D0",5}}
  113. ],
  114. expand_handlers([{lager_console_backend, info},
  115. {lager_file_backend, [
  116. {"error.log", error, 10485760, "$D0", 5},
  117. {"console.log", info, 10485760, "$D0", 5}
  118. ]}]
  119. ))},
  120. {"Explode with formatter info",
  121. ?_assertMatch(
  122. [{{lager_file_backend,"test.log"}, [{"test.log", debug, 10485760, "$D0", 5},{lager_default_formatter,["[",severity,"] ", message, "\n"]}]},
  123. {{lager_file_backend,"test2.log"}, [{"test2.log",debug, 10485760, "$D0", 5},{lager_default_formatter,["2>[",severity,"] ", message, "\n"]}]}],
  124. expand_handlers([{lager_file_backend, [
  125. [{"test.log", debug, 10485760, "$D0", 5},{lager_default_formatter,["[",severity,"] ", message, "\n"]}],
  126. [{"test2.log",debug, 10485760, "$D0", 5},{lager_default_formatter,["2>[",severity,"] ",message, "\n"]}]
  127. ]
  128. }])
  129. )
  130. }].
  131. -endif.