Here's an example of how to do it.
def with_performance_log(options, body, context) do
quote bind_quoted: [
context: Macro.escape(context),
options: Macro.escape(options),
body: body
] do
module = "#{context.module}"
function = "#{context.name}/#{context.arity}"
level = options[:level] || :debug
message = options[:message] || "undefined"
metadata = options[:metadata] || []
{time_us, value} = :timer.tc(fn -> body end)
metadata =
[
module: module,
function: function,
time_us: time_us
] ++ metadata
Logger.bare_log(level, message, metadata)
value
end
end
Documentation for the library can be checked at https://hexdocs.pm/decorator/readme.html.