logger.hrl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(TRACE_FILTER, emqx_trace_filter).
  40. -define(TRACE(Tag, Msg, Meta), ?TRACE(debug, Tag, Msg, Meta)).
  41. %% Only evaluate when necessary
  42. -define(TRACE(Level, Tag, Msg, Meta), begin
  43. case persistent_term:get(?TRACE_FILTER, []) of
  44. [] -> ok;
  45. %% We can't bind filter list to a variable because we pollute the calling scope with it.
  46. %% We also don't want to wrap the macro body in a fun
  47. %% because this adds overhead to the happy path.
  48. %% So evaluate `persistent_term:get` twice.
  49. _ -> emqx_trace:log(persistent_term:get(?TRACE_FILTER, []), Msg, (Meta)#{trace_tag => Tag})
  50. end,
  51. ?SLOG(
  52. Level,
  53. (emqx_trace_formatter:format_meta_map(Meta))#{msg => Msg, tag => Tag},
  54. #{is_trace => false}
  55. )
  56. end).
  57. -define(AUDIT(_Level_, _From_, _Meta_), begin
  58. case emqx_config:get([log, audit], #{enable => false}) of
  59. #{enable := false} ->
  60. ok;
  61. #{enable := true, level := _AllowLevel_} ->
  62. case logger:compare_levels(_AllowLevel_, _Level_) of
  63. _R_ when _R_ == lt; _R_ == eq ->
  64. emqx_trace:log(
  65. _Level_,
  66. [{emqx_audit, fun(L, _) -> L end, undefined, undefined}],
  67. _Msg = undefined,
  68. _Meta_#{from => _From_}
  69. );
  70. gt ->
  71. ok
  72. end
  73. end
  74. end).
  75. %% print to 'user' group leader
  76. -define(ULOG(Fmt, Args), io:format(user, Fmt, Args)).
  77. -define(ELOG(Fmt, Args), io:format(standard_error, Fmt, Args)).
  78. -endif.