Skip to content

Commit

Permalink
Let count_next count logs per level instead of total
Browse files Browse the repository at this point in the history
  • Loading branch information
mnishiguchi committed Nov 17, 2022
1 parent 14fb2fa commit e3ada54
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
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

0 comments on commit e3ada54

Please sign in to comment.