Skip to content

Commit

Permalink
Fix crash when grep'ing iodata
Browse files Browse the repository at this point in the history
Log messages are technically iodata, so they need to be flattened to
normal strings before sending them through Regex.match?/2.
  • Loading branch information
fhunleth authored and mobileoverlord committed Apr 15, 2018
1 parent a12ed1f commit a7c04cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/ring_logger/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ defmodule RingLogger.Client do
end

defp maybe_print({level, {_, text, _, _}} = msg, r, state) do
if meet_level?(level, state.level) && Regex.match?(r, text) do
flattened_text = IO.iodata_to_binary(text)

if meet_level?(level, state.level) && Regex.match?(r, flattened_text) do
item = format_message(msg, state)
IO.binwrite(state.io, item)
end
Expand Down
8 changes: 8 additions & 0 deletions test/ring_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ defmodule RingLoggerTest do
assert message =~ "[debug] Hello"
end

test "can grep iodata in log", %{io: io} do
Logger.debug(["Hello", ",", ' world'])
Logger.debug("World")
RingLogger.grep(~r/H..lo/, io: io)
assert_receive {:io, message}
assert message =~ "[debug] Hello, world"
end

test "can tail the log", %{io: io} do
Logger.debug("Hello")
:ok = RingLogger.tail(io: io)
Expand Down

0 comments on commit a7c04cb

Please sign in to comment.