A function decorator macro for Elixir. Used mainly for adding log statements to the function calls.
This project is based mainly on:
- The work of Saša Jurić on the Elixir macro articles. Especially the mechanism for extracting function definition metadata.
- The solution from him for how to override the def macro in Elixir
- An addition made by Björn Rochel to the adef macro considering default values for arguments.
All I did was use the solution provided by the mentioned above, add some minor refactoring and adjustments for my needs and package it as an helper module.
This was a learning project getting into Elixir macros field.
For getting into Elixir Macros, you are encouraged to read Saša Jurić 's excellent Elixir macro articles series
- Currently in alpha stage.
- Not intended to be used in production. Only for experiments.
- For logging, one can use other means like trace.
The package can be installed as:
-
Add function_decorating to your list of dependencies in
mix.exs
:def deps do [{:function_decorating, "~> 0.0.6"}] end
-
Ensure function_decorating is started before your application:
def application do [applications: [:function_decorating]] end
Decorating in dev with log decorator.
defmodule User do
use FunctionDecorating
decorate_fn_with(LogDecorator)
def say(word) do
word
end
end
iex>User.say("hello")
#PID<0.86.0> [x] Elixir.User.say(["hello"]) -> "hello"
"hello"
Default usage is for Mix.env == :dev only. To override it:
defmodule User do
use FunctionDecorating mix_envs: [:test]
decorate_fn_with(LogDecorator)
def say(word) do
word
end
end
iex >Mix.env
:test
iex >User.say("hello")
#PID<0.86.0> [x] Elixir.User.say(["hello"]) -> "hello"
"hello"