Skip to content

Instantly share code, notes, and snippets.

@potatosalad
Created August 17, 2018 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potatosalad/6e523152b4f407d6aacc11f122a885d4 to your computer and use it in GitHub Desktop.
Save potatosalad/6e523152b4f407d6aacc11f122a885d4 to your computer and use it in GitHub Desktop.
defmodule ErlangLogger do
@moduledoc """
Designed to mirror [`kernel/include/logger.hrl`](https://github.com/erlang/otp/blob/da166b76de977171243dd2ea7f86b98f451fabc5/lib/kernel/include/logger.hrl)
*WARNING:* Currently blows up if `__CALLER__` does not have a valid `mfa`
"""
defmacro log_emergency(a) do
do_log(:emergency, [a], __CALLER__)
end
defmacro log_emergency(a, b) do
do_log(:emergency, [a, b], __CALLER__)
end
defmacro log_emergency(a, b, c) do
do_log(:emergency, [a, b, c], __CALLER__)
end
defmacro log_alert(a) do
do_log(:alert, [a], __CALLER__)
end
defmacro log_alert(a, b) do
do_log(:alert, [a, b], __CALLER__)
end
defmacro log_alert(a, b, c) do
do_log(:alert, [a, b, c], __CALLER__)
end
defmacro log_critical(a) do
do_log(:critical, [a], __CALLER__)
end
defmacro log_critical(a, b) do
do_log(:critical, [a, b], __CALLER__)
end
defmacro log_critical(a, b, c) do
do_log(:critical, [a, b, c], __CALLER__)
end
defmacro log_error(a) do
do_log(:error, [a], __CALLER__)
end
defmacro log_error(a, b) do
do_log(:error, [a, b], __CALLER__)
end
defmacro log_error(a, b, c) do
do_log(:error, [a, b, c], __CALLER__)
end
defmacro log_warning(a) do
do_log(:warning, [a], __CALLER__)
end
defmacro log_warning(a, b) do
do_log(:warning, [a, b], __CALLER__)
end
defmacro log_warning(a, b, c) do
do_log(:warning, [a, b, c], __CALLER__)
end
defmacro log_notice(a) do
do_log(:notice, [a], __CALLER__)
end
defmacro log_notice(a, b) do
do_log(:notice, [a, b], __CALLER__)
end
defmacro log_notice(a, b, c) do
do_log(:notice, [a, b, c], __CALLER__)
end
defmacro log_info(a) do
do_log(:info, [a], __CALLER__)
end
defmacro log_info(a, b) do
do_log(:info, [a, b], __CALLER__)
end
defmacro log_info(a, b, c) do
do_log(:info, [a, b, c], __CALLER__)
end
defmacro log_debug(a) do
do_log(:debug, [a], __CALLER__)
end
defmacro log_debug(a, b) do
do_log(:debug, [a, b], __CALLER__)
end
defmacro log_debug(a, b, c) do
do_log(:debug, [a, b, c], __CALLER__)
end
@doc false
defp do_log(level, args, caller) do
%{module: module, function: {function, arity}, file: file, line: line} = caller
location = Macro.escape(%{mfa: {module, function, arity}, line: line, file: file})
quote do
case :logger.allow(unquote(level), unquote(module)) do
true ->
apply(:logger, :macro_log, [unquote(location), unquote(level) | unquote(args)])
false ->
:ok
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment