Skip to content

Commit

Permalink
Make logging on halt optional
Browse files Browse the repository at this point in the history
Through the `:log_on_halt` option passed to `use Plug.Builder`.
  • Loading branch information
whatyouhide committed Apr 5, 2015
1 parent 6daf94a commit 52d3315
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
23 changes: 14 additions & 9 deletions lib/plug/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ defmodule Plug.Builder do
@type plug :: module | atom

@doc false
defmacro __using__(_) do
defmacro __using__(opts) do
quote do
@behaviour Plug
@plug_builder_opts unquote(opts)

def init(opts) do
opts
Expand All @@ -119,13 +120,14 @@ defmodule Plug.Builder do

@doc false
defmacro __before_compile__(env) do
plugs = Module.get_attribute(env.module, :plugs)
plugs = Module.get_attribute(env.module, :plugs)
builder_opts = Module.get_attribute(env.module, :plug_builder_opts)

if plugs == [] do
raise "no plugs have been defined in #{inspect env.module}"
end

{conn, body} = Plug.Builder.compile(plugs)
{conn, body} = Plug.Builder.compile(plugs, builder_opts)

quote do
defp plug_builder_call(unquote(conn), _), do: unquote(body)
Expand Down Expand Up @@ -173,10 +175,10 @@ defmodule Plug.Builder do
])
"""
@spec compile([{plug, Plug.opts, Macro.t}]) :: {Macro.t, Macro.t}
def compile(pipeline) do
@spec compile([{plug, Plug.opts, Macro.t}], Keyword.t) :: {Macro.t, Macro.t}
def compile(pipeline, builder_opts \\ []) do
conn = quote do: conn
{conn, Enum.reduce(pipeline, conn, &quote_plug(init_plug(&1), &2))}
{conn, Enum.reduce(pipeline, conn, &quote_plug(init_plug(&1), &2, builder_opts))}
end

# Initializes the options of a plug at compile time.
Expand Down Expand Up @@ -204,7 +206,7 @@ defmodule Plug.Builder do
# `acc` is a series of nested plug calls in the form of
# plug3(plug2(plug1(conn))). `quote_plug` wraps a new plug around that series
# of calls.
defp quote_plug({plug_type, plug, opts, guards}, acc) do
defp quote_plug({plug_type, plug, opts, guards}, acc, builder_opts) do
call = quote_plug_call(plug_type, plug, opts)

error_message = case plug_type do
Expand All @@ -220,8 +222,11 @@ defmodule Plug.Builder do
quote do
case unquote(compile_guards(call, guards)) do
%Plug.Conn{halted: true} = conn ->
require Logger
Logger.debug unquote(halt_message)
if log_on_halt_level = unquote(builder_opts[:log_on_halt]) do
require Logger
Logger.log(log_on_halt_level, unquote(halt_message))
end

conn
%Plug.Conn{} = conn ->
unquote(acc)
Expand Down
2 changes: 1 addition & 1 deletion test/plug/builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule Plug.BuilderTest do
end

defmodule Halter do
use Plug.Builder
use Plug.Builder, log_on_halt: :debug

plug :step, :first
plug :step, :second
Expand Down
4 changes: 0 additions & 4 deletions test/plug/static_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ defmodule Plug.StaticTest do
use ExUnit.Case, async: true
use Plug.Test

setup do
Logger.disable(self)
end

defmodule MyPlug do
use Plug.Builder

Expand Down

0 comments on commit 52d3315

Please sign in to comment.