logger.hrl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. -define(LOG(Level, Format, Args, Meta),
  35. %% check 'allow' here so we do not have to pass an anonymous function
  36. %% down to logger which may cause `badfun` exception during upgrade
  37. case logger:allow(Level, ?MODULE) of
  38. true ->
  39. logger:log(Level, (Format), (Args),
  40. (Meta)#{ mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}
  41. , line => ?LINE
  42. });
  43. false ->
  44. ok
  45. end).
  46. -define(LOG(Level, Format, Args), ?LOG(Level, Format, Args, #{})).
  47. %% structured logging
  48. -define(SLOG(Level, Data),
  49. logger:log(Level, Data, #{ mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}
  50. , line => ?LINE})).
  51. %% print to 'user' group leader
  52. -define(ULOG(Fmt, Args), io:format(user, Fmt, Args)).
  53. -define(ELOG(Fmt, Args), io:format(standard_error, Fmt, Args)).
  54. -endif.