logger.hrl 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. %% debug | info | notice | warning | error | critical | alert | emergency
  19. -define(DEBUG(Format), ?LOG(debug, Format, [])).
  20. -define(DEBUG(Format, Args), ?LOG(debug, Format, Args)).
  21. -define(INFO(Format), ?LOG(info, Format, [])).
  22. -define(INFO(Format, Args), ?LOG(info, Format, Args)).
  23. -define(NOTICE(Format), ?LOG(notice, Format, [])).
  24. -define(NOTICE(Format, Args), ?LOG(notice, Format, Args)).
  25. -define(WARN(Format), ?LOG(warning, Format, [])).
  26. -define(WARN(Format, Args), ?LOG(warning, Format, Args)).
  27. -define(ERROR(Format), ?LOG(error, Format, [])).
  28. -define(ERROR(Format, Args), ?LOG(error, Format, Args)).
  29. -define(CRITICAL(Format), ?LOG(critical, Format, [])).
  30. -define(CRITICAL(Format, Args), ?LOG(critical, Format, Args)).
  31. -define(ALERT(Format), ?LOG(alert, Format, [])).
  32. -define(ALERT(Format, Args), ?LOG(alert, Format, Args)).
  33. -define(LOG(Level, Format), ?LOG(Level, Format, [])).
  34. %% deprecated
  35. -define(LOG(Level, Format, Args, Meta),
  36. %% check 'allow' here so we do not have to pass an anonymous function
  37. %% down to logger which may cause `badfun` exception during upgrade
  38. case logger:allow(Level, ?MODULE) of
  39. true ->
  40. logger:log(Level, (Format), (Args),
  41. (Meta)#{ mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}
  42. , line => ?LINE
  43. });
  44. false ->
  45. ok
  46. end).
  47. -define(LOG(Level, Format, Args), ?LOG(Level, Format, Args, #{})).
  48. %% structured logging
  49. -define(SLOG(Level, Data),
  50. ?SLOG(Level, Data, #{})).
  51. %% structured logging, meta is for handler's filter.
  52. -define(SLOG(Level, Data, Meta),
  53. %% check 'allow' here, only evaluate Data and Meta when necessary
  54. case logger:allow(Level, ?MODULE) of
  55. true ->
  56. logger:log(Level, (Data), (Meta#{ mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}
  57. , line => ?LINE
  58. }));
  59. false ->
  60. ok
  61. end).
  62. -define(TRACE_FILTER, emqx_trace_filter).
  63. %% Only evaluate when necessary
  64. %% Always debug the trace events.
  65. -define(TRACE(Tag, Msg, Meta),
  66. begin
  67. case persistent_term:get(?TRACE_FILTER, undefined) of
  68. undefined -> ok;
  69. [] -> ok;
  70. List -> emqx_trace:log(List, Msg, Meta#{trace_tag => Tag})
  71. end,
  72. ?SLOG(debug, (emqx_trace_formatter:format_meta(Meta))#{msg => Msg, tag => Tag},
  73. #{is_trace => false})
  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.