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.

178 rivejä
7.1 KiB

14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
14 vuotta sitten
13 vuotta sitten
14 vuotta sitten
14 vuotta sitten
  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. -define(DEFAULT_TRUNCATION, 4096).
  17. -define(DEFAULT_TRACER, lager_default_tracer).
  18. -define(DEFAULT_SINK, lager_event).
  19. -define(ERROR_LOGGER_SINK, error_logger_lager_event).
  20. -define(METADATA(Extras), [{severity, info},
  21. {pid, self()},
  22. {node, node()},
  23. {module, ?MODULE},
  24. {function, ?FUNCTION_NAME},
  25. {function_arity, ?FUNCTION_ARITY},
  26. {file, ?FILE},
  27. {line, ?LINE} | Extras]).
  28. -define(lager_log(Severity, Format, Args, Safety),
  29. ?lager_log(?DEFAULT_SINK, Severity, ?METADATA(lager:md()), Format, Args,
  30. ?DEFAULT_TRUNCATION, Safety)).
  31. -define(lager_log(Severity, Metadata, Format, Args, Safety),
  32. ?lager_log(?DEFAULT_SINK, Severity, ?METADATA(Metadata++lager:md()), Format, Args,
  33. ?DEFAULT_TRUNCATION, Safety)).
  34. -define(lager_log(Sink, Severity, Metadata, Format, Args, Size, Safety),
  35. _ = lager:dispatch_log(Sink, Severity, Metadata, Format, Args, Size, Safety)).
  36. -define(lager_debug(Format, Args), ?lager_log(debug, Format, Args, safe)).
  37. -define(lager_debug(Metadata, Format, Args), ?lager_log(debug, Metadata, Format, Args, safe)).
  38. -define(lager_info(Format, Args), ?lager_log(info, Format, Args, safe)).
  39. -define(lager_info(Metadata, Format, Args), ?lager_log(info, Metadata, Format, Args, safe)).
  40. -define(lager_notice(Format, Args), ?lager_log(notice, Format, Args, safe)).
  41. -define(lager_notice(Metadata, Format, Args), ?lager_log(notice, Metadata, Format, Args, safe)).
  42. -define(lager_warning(Format, Args), ?lager_log(warning, Format, Args, safe)).
  43. -define(lager_warning(Metadata, Format, Args), ?lager_log(warning, Metadata, Format, Args, safe)).
  44. -define(lager_error(Format, Args), ?lager_log(error, Format, Args, safe)).
  45. -define(lager_error(Metadata, Format, Args), ?lager_log(error, Metadata, Format, Args, safe)).
  46. -define(lager_critical(Format, Args), ?lager_log(critical, Format, Args, safe)).
  47. -define(lager_critical(Metadata, Format, Args), ?lager_log(critical, Metadata, Format, Args, safe)).
  48. -define(lager_alert(Format, Args), ?lager_log(alert, Format, Args, safe)).
  49. -define(lager_alert(Metadata, Format, Args), ?lager_log(alert, Metadata, Format, Args, safe)).
  50. -define(lager_emergency(Format, Args), ?lager_log(emergency, Format, Args, safe)).
  51. -define(lager_emergency(Metadata, Format, Args), ?lager_log(emergency, Metadata, Format, Args, safe)).
  52. -define(lager_none(Format, Args), ?lager_log(none, Format, Args, safe)).
  53. -define(lager_none(Metadata, Format, Args), ?lager_log(none, Metadata, Format, Args, safe)).
  54. -define(LEVELS,
  55. [debug, info, notice, warning, error, critical, alert, emergency, none]).
  56. %% Use of these "functions" means that the argument list will not be
  57. %% truncated for safety
  58. -define(LEVELS_UNSAFE,
  59. [{debug_unsafe, debug}, {info_unsafe, info}, {notice_unsafe, notice}, {warning_unsafe, warning}, {error_unsafe, error}, {critical_unsafe, critical}, {alert_unsafe, alert}, {emergency_unsafe, emergency}]).
  60. -define(DEBUG, 128).
  61. -define(INFO, 64).
  62. -define(NOTICE, 32).
  63. -define(WARNING, 16).
  64. -define(ERROR, 8).
  65. -define(CRITICAL, 4).
  66. -define(ALERT, 2).
  67. -define(EMERGENCY, 1).
  68. -define(LOG_NONE, 0).
  69. -define(LEVEL2NUM(Level),
  70. case Level of
  71. debug -> ?DEBUG;
  72. info -> ?INFO;
  73. notice -> ?NOTICE;
  74. warning -> ?WARNING;
  75. error -> ?ERROR;
  76. critical -> ?CRITICAL;
  77. alert -> ?ALERT;
  78. emergency -> ?EMERGENCY
  79. end).
  80. -define(NUM2LEVEL(Num),
  81. case Num of
  82. ?DEBUG -> debug;
  83. ?INFO -> info;
  84. ?NOTICE -> notice;
  85. ?WARNING -> warning;
  86. ?ERROR -> error;
  87. ?CRITICAL -> critical;
  88. ?ALERT -> alert;
  89. ?EMERGENCY -> emergency
  90. end).
  91. -define(SHOULD_LOG(Sink, Level),
  92. (lager_util:level_to_num(Level) band element(1, lager_config:get({Sink, loglevel}, {?LOG_NONE, []}))) /= 0).
  93. -define(SHOULD_LOG(Level),
  94. (lager_util:level_to_num(Level) band element(1, lager_config:get(loglevel, {?LOG_NONE, []}))) /= 0).
  95. -define(NOTIFY(Level, Pid, Format, Args),
  96. gen_event:notify(lager_event, {log, lager_msg:new(io_lib:format(Format, Args),
  97. Level,
  98. [{pid,Pid},{line,?LINE},{file,?FILE},{module,?MODULE}],
  99. [])}
  100. )).
  101. %% FOR INTERNAL USE ONLY
  102. %% internal non-blocking logging call
  103. %% there's some special handing for when we try to log (usually errors) while
  104. %% lager is still starting.
  105. -ifdef(TEST).
  106. -define(INT_LOG(Level, Format, Args),
  107. case ?SHOULD_LOG(Level) of
  108. true ->
  109. ?NOTIFY(Level, self(), Format, Args);
  110. _ ->
  111. ok
  112. end).
  113. -else.
  114. -define(INT_LOG(Level, Format, Args),
  115. Self = self(),
  116. %% do this in a spawn so we don't cause a deadlock calling gen_event:which_handlers
  117. %% from a gen_event handler
  118. spawn(fun() ->
  119. case catch(gen_event:which_handlers(lager_event)) of
  120. X when X == []; X == {'EXIT', noproc}; X == [lager_backend_throttle] ->
  121. %% there's no handlers yet or lager isn't running, try again
  122. %% in half a second.
  123. timer:sleep(500),
  124. ?NOTIFY(Level, Self, Format, Args);
  125. _ ->
  126. case ?SHOULD_LOG(Level) of
  127. true ->
  128. ?NOTIFY(Level, Self, Format, Args);
  129. _ ->
  130. ok
  131. end
  132. end
  133. end)).
  134. -endif.
  135. -record(lager_shaper, {
  136. id :: any(),
  137. %% how many messages per second we try to deliver
  138. hwm = undefined :: 'undefined' | pos_integer(),
  139. %% how many messages we've received this second
  140. mps = 0 :: non_neg_integer(),
  141. %% the current second
  142. lasttime = os:timestamp() :: erlang:timestamp(),
  143. %% count of dropped messages this second
  144. dropped = 0 :: non_neg_integer(),
  145. %% If true, flush notify messages from msg queue at overload
  146. flush_queue = true :: boolean(),
  147. flush_threshold = 0 :: integer(),
  148. %% timer
  149. timer = make_ref() :: reference(),
  150. %% optional filter fun to avoid counting suppressed messages against HWM totals
  151. filter = fun(_) -> false end :: fun()
  152. }).
  153. -type lager_shaper() :: #lager_shaper{}.