Elixir Function Decorator

A function decorator macro for Elixir. Used mainly for adding log statements to the function calls.

This project is based mainly on:

Motivation

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

State

  • Currently in alpha stage.
  • Not intended to be used in production. Only for experiments.
  • For logging, one can use other means like trace.

Installation

The package can be installed as:

  1. Add function_decorating to your list of dependencies in mix.exs:

    def deps do
      [{:function_decorating, "~> 0.0.5"}]
    end
  2. Ensure function_decorating is started before your application:

    def application do
      [applications: [:function_decorating]]
    end

Usage

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"