Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let count_next count logs per level instead of total #101

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/ring_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
18 changes: 13 additions & 5 deletions lib/ring_logger/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/ring_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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.()
Expand Down