logger.hrl 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2018-2021 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. %% check 'allow' here, only evaluate Data when necessary
  51. case logger:allow(Level, ?MODULE) of
  52. true ->
  53. logger:log(Level, (Data), #{ mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}
  54. , line => ?LINE
  55. });
  56. false ->
  57. ok
  58. end).
  59. %% print to 'user' group leader
  60. -define(ULOG(Fmt, Args), io:format(user, Fmt, Args)).
  61. -define(ELOG(Fmt, Args), io:format(standard_error, Fmt, Args)).
  62. -endif.