diff --git a/lib/ring_logger.ex b/lib/ring_logger.ex index 81a5b7e..045b7ef 100644 --- a/lib/ring_logger.ex +++ b/lib/ring_logger.ex @@ -112,7 +112,14 @@ defmodule RingLogger do * Options from `attach/1` * `:pager` - a function for printing log messages to the console. Defaults to `IO.binwrite/2`. """ - @spec count_next([client_option]) :: non_neg_integer() | {:error, term()} + @spec count_next([client_option]) :: + %{ + info: non_neg_integer(), + debug: non_neg_integer(), + warn: non_neg_integer(), + error: non_neg_integer() + } + | {:error, term()} defdelegate count_next(opts \\ []), to: Autoclient @doc """ diff --git a/lib/ring_logger/client.ex b/lib/ring_logger/client.ex index 73f5794..6373c01 100644 --- a/lib/ring_logger/client.ex +++ b/lib/ring_logger/client.ex @@ -118,9 +118,16 @@ defmodule RingLogger.Client do end @doc """ - Count the next set of the messages in the log. + Get the per-level message counts for the next set of the messages in the log. """ - @spec count_next(GenServer.server()) :: non_neg_integer() | {:error, term()} + @spec count_next(GenServer.server()) :: + %{ + info: non_neg_integer(), + debug: non_neg_integer(), + warn: non_neg_integer(), + error: non_neg_integer() + } + | {:error, term()} def count_next(client_pid) do GenServer.call(client_pid, :count_next) end @@ -235,11 +242,12 @@ defmodule RingLogger.Client do end def handle_call(:count_next, _from, state) do - count = + counts = Server.get(state.index, 0) - |> Enum.count(&should_print?(&1, state)) + |> Enum.filter(&should_print?(&1, state)) + |> Enum.frequencies_by(&elem(&1, 0)) - {:reply, count, state} + {:reply, counts, state} end def handle_call({:tail, n}, _from, state) do diff --git a/test/ring_logger_test.exs b/test/ring_logger_test.exs index d117ede..dab68ee 100644 --- a/test/ring_logger_test.exs +++ b/test/ring_logger_test.exs @@ -499,6 +499,22 @@ defmodule RingLoggerTest do end end + describe "count_next/1" do + test "returns per-level log counts for next set of log messages" do + assert RingLogger.count_next() == %{} + Logger.info('foo') + assert RingLogger.count_next() == %{info: 1} + Logger.debug('bar') + assert RingLogger.count_next() == %{info: 1, debug: 1} + Logger.warn('baz') + assert RingLogger.count_next() == %{info: 1, debug: 1, warn: 1} + Logger.error('uhh') + assert RingLogger.count_next() == %{info: 1, debug: 1, warn: 1, error: 1} + Logger.info('foo') + assert RingLogger.count_next() == %{info: 2, debug: 1, warn: 1, error: 1} + end + end + defp capture_log(fun) do capture_io(:user, fn -> fun.()