rewrite from lager
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.

92 lines
3.0 KiB

пре 4 година
  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 top level supervisor.
  17. %% @private
  18. -module(lager_sup).
  19. -behaviour(supervisor).
  20. %% API
  21. -export([start_link/0]).
  22. %% Callbacks
  23. -export([init/1]).
  24. start_link() ->
  25. supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  26. init([]) ->
  27. %% set up the config, is safe even during relups
  28. lager_config:new(),
  29. %% TODO:
  30. %% Always start lager_event as the default and make sure that
  31. %% other gen_event stuff can start up as needed
  32. %%
  33. %% Maybe a new API to handle the sink and its policy?
  34. Children = [
  35. {lager, {gen_event, start_link, [{local, lager_event}]},
  36. permanent, 5000, worker, dynamic},
  37. {lager_handler_watcher_sup, {lager_handler_watcher_sup, start_link, []},
  38. permanent, 5000, supervisor, [lager_handler_watcher_sup]}],
  39. CrashLog = decide_crash_log(application:get_env(lager, crash_log, false)),
  40. {ok, {{one_for_one, 10, 60},
  41. Children ++ CrashLog
  42. }}.
  43. validate_positive({ok, Val}, _Default) when is_integer(Val) andalso Val >= 0 ->
  44. Val;
  45. validate_positive(_Val, Default) ->
  46. Default.
  47. determine_rotation_date({ok, ""}) ->
  48. undefined;
  49. determine_rotation_date({ok, Val3}) ->
  50. case lager_util:parse_rotation_date_spec(Val3) of
  51. {ok, Spec} -> Spec;
  52. {error, _} ->
  53. error_logger:error_msg("Invalid date spec for "
  54. "crash log ~p~n", [Val3]),
  55. undefined
  56. end;
  57. determine_rotation_date(_) ->
  58. undefined.
  59. determine_rotator_mod({ok, Mod}, _Default) when is_atom(Mod) ->
  60. Mod;
  61. determine_rotator_mod(_, Default) ->
  62. Default.
  63. decide_crash_log(undefined) ->
  64. [];
  65. decide_crash_log(false) ->
  66. [];
  67. decide_crash_log(File) ->
  68. MaxBytes = validate_positive(application:get_env(lager, crash_log_msg_size), 65536),
  69. RotationSize = validate_positive(application:get_env(lager, crash_log_size), 0),
  70. RotationCount = validate_positive(application:get_env(lager, crash_log_count), 0),
  71. RotationDate = determine_rotation_date(application:get_env(lager, crash_log_date)),
  72. RotationMod = determine_rotator_mod(application:get_env(lager, crash_log_rotator), lager_rotator_default),
  73. [{lager_crash_log, {lager_crash_log, start_link, [File, MaxBytes,
  74. RotationSize, RotationDate, RotationCount, RotationMod]},
  75. permanent, 5000, worker, [lager_crash_log]}].