BoxTurtle.Debug.PipeDebug (BoxTurtle v0.1.0)

Copy Markdown View Source

Injects a debug/3 function that can be dropped into the middle of a pipe to log a value, returning it unchanged.

Logging is off by default and enabled per module through application configuration, so debug calls can stay in the code without flooding the logs.

Usage

defmodule MyApp.Billing do
  use BoxTurtle.Debug.PipeDebug

  def invoice_total(items) do
    items
    |> Enum.map(& &1.price)
    |> debug("prices")
    |> Enum.sum()
    # Only log suspicious totals.
    |> debug("large total", condition: &(&1 > 10_000))
  end
end

# config/dev.exs: log from these modules only
config :my_app, debug_enabled_modules: [MyApp.Billing]

# Logger must also let :debug through (the default in dev):
config :logger, level: :debug

With that config, MyApp.Billing.invoice_total([%{price: 5}, %{price: 7}]) returns 12 and logs prices: [5, 7]. With MyApp.Billing not listed, it returns 12 and logs nothing.

To log at :info instead, or to pick the application whose config is read:

use BoxTurtle.Debug.PipeDebug, application: :my_app, logger_macro: :info

Options

  • :application - the application whose :debug_enabled_modules setting is consulted. Defaults to the application the using module belongs to, falling back to :box_turtle. Useful in tests, where modules may not belong to a loaded application.
  • :logger_module - defaults to Logger. Any module exposing debug/1 and info/1 (functions or macros) will do; mostly used for testing PipeDebug itself.
  • :logger_macro - :debug (default) or :info, the level messages are logged at.