logger.hrl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2018-2022 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. %% Only evaluate when necessary
  41. %% Always debug the trace events.
  42. -define(TRACE(Tag, Msg, Meta), begin
  43. case persistent_term:get(?TRACE_FILTER, undefined) of
  44. undefined -> ok;
  45. [] -> ok;
  46. List -> emqx_trace:log(List, Msg, Meta#{trace_tag => Tag})
  47. end,
  48. ?SLOG(
  49. debug,
  50. (emqx_trace_formatter:format_meta(Meta))#{msg => Msg, tag => Tag},
  51. #{is_trace => false}
  52. )
  53. end).
  54. %% print to 'user' group leader
  55. -define(ULOG(Fmt, Args), io:format(user, Fmt, Args)).
  56. -define(ELOG(Fmt, Args), io:format(standard_error, Fmt, Args)).
  57. -endif.