logger.hrl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2018-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain 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, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -ifndef(EMQX_LOGGER_HRL).
  17. -define(EMQX_LOGGER_HRL, true).
  18. %% structured logging
  19. -define(SLOG(Level, Data),
  20. ?SLOG(Level, Data, #{})
  21. ).
  22. %% structured logging, meta is for handler's filter.
  23. -define(SLOG(Level, Data, Meta),
  24. %% check 'allow' here, only evaluate Data and Meta when necessary
  25. case logger:allow(Level, ?MODULE) of
  26. true ->
  27. logger:log(
  28. Level,
  29. (Data),
  30. (Meta#{
  31. mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY},
  32. line => ?LINE
  33. })
  34. );
  35. false ->
  36. ok
  37. end
  38. ).
  39. -define(AUDIT_HANDLER, emqx_audit).
  40. -define(TRACE_FILTER, emqx_trace_filter).
  41. -define(OWN_KEYS, [level, filters, filter_default, handlers]).
  42. -define(TRACE(Tag, Msg, Meta), ?TRACE(debug, Tag, Msg, Meta)).
  43. %% Only evaluate when necessary
  44. -define(TRACE(Level, Tag, Msg, Meta), begin
  45. case persistent_term:get(?TRACE_FILTER, []) of
  46. [] -> ok;
  47. %% We can't bind filter list to a variable because we pollute the calling scope with it.
  48. %% We also don't want to wrap the macro body in a fun
  49. %% because this adds overhead to the happy path.
  50. %% So evaluate `persistent_term:get` twice.
  51. _ -> emqx_trace:log(persistent_term:get(?TRACE_FILTER, []), Msg, (Meta)#{trace_tag => Tag})
  52. end,
  53. ?SLOG(
  54. Level,
  55. (emqx_trace_formatter:format_meta_map(Meta))#{msg => Msg, tag => Tag},
  56. #{is_trace => false}
  57. )
  58. end).
  59. -ifdef(EMQX_RELEASE_EDITION).
  60. -if(?EMQX_RELEASE_EDITION == ee).
  61. -define(AUDIT(_LevelFun_, _MetaFun_), begin
  62. case logger_config:get(logger, ?AUDIT_HANDLER) of
  63. {error, {not_found, _}} ->
  64. ok;
  65. {ok, Handler = #{level := _AllowLevel_}} ->
  66. _Level_ = _LevelFun_,
  67. case logger:compare_levels(_AllowLevel_, _Level_) of
  68. _R_ when _R_ == lt; _R_ == eq ->
  69. emqx_audit:log(_Level_, _MetaFun_, Handler);
  70. _ ->
  71. ok
  72. end
  73. end
  74. end).
  75. -else.
  76. %% Only for compile pass, ce edition will not call it
  77. -define(AUDIT(_L_, _M_), _ = {_L_, _M_}).
  78. -endif.
  79. -else.
  80. %% Only for compile pass, ce edition will not call it
  81. -define(AUDIT(_L_, _M_), _ = {_L_, _M_}).
  82. -endif.
  83. %% print to 'user' group leader
  84. -define(ULOG(Fmt, Args), io:format(user, Fmt, Args)).
  85. -define(ELOG(Fmt, Args), io:format(standard_error, Fmt, Args)).
  86. -endif.