logger.hrl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. %% structured logging, meta is for handler's filter.
  22. -define(SLOG(Level, Data, Meta),
  23. %% check 'allow' here, only evaluate Data and Meta when necessary
  24. case logger:allow(Level, ?MODULE) of
  25. true ->
  26. logger:log(Level, (Data), (Meta#{ mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}
  27. , line => ?LINE
  28. }));
  29. false ->
  30. ok
  31. end).
  32. -define(TRACE_FILTER, emqx_trace_filter).
  33. %% Only evaluate when necessary
  34. %% Always debug the trace events.
  35. -define(TRACE(Tag, Msg, Meta),
  36. begin
  37. case persistent_term:get(?TRACE_FILTER, undefined) of
  38. undefined -> ok;
  39. [] -> ok;
  40. List -> emqx_trace:log(List, Msg, Meta#{trace_tag => Tag})
  41. end,
  42. ?SLOG(debug, (emqx_trace_formatter:format_meta(Meta))#{msg => Msg, tag => Tag},
  43. #{is_trace => false})
  44. end).
  45. %% print to 'user' group leader
  46. -define(ULOG(Fmt, Args), io:format(user, Fmt, Args)).
  47. -define(ELOG(Fmt, Args), io:format(standard_error, Fmt, Args)).
  48. -endif.