Skip to content

Commit e3ada54

Browse files
committed
Let count_next count logs per level instead of total
1 parent 14fb2fa commit e3ada54

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

lib/ring_logger.ex

+8-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,14 @@ defmodule RingLogger do
112112
* Options from `attach/1`
113113
* `:pager` - a function for printing log messages to the console. Defaults to `IO.binwrite/2`.
114114
"""
115-
@spec count_next([client_option]) :: non_neg_integer() | {:error, term()}
115+
@spec count_next([client_option]) ::
116+
%{
117+
info: non_neg_integer(),
118+
debug: non_neg_integer(),
119+
warn: non_neg_integer(),
120+
error: non_neg_integer()
121+
}
122+
| {:error, term()}
116123
defdelegate count_next(opts \\ []), to: Autoclient
117124

118125
@doc """

lib/ring_logger/client.ex

+13-5
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,16 @@ defmodule RingLogger.Client do
118118
end
119119

120120
@doc """
121-
Count the next set of the messages in the log.
121+
Get the per-level message counts for the next set of the messages in the log.
122122
"""
123-
@spec count_next(GenServer.server()) :: non_neg_integer() | {:error, term()}
123+
@spec count_next(GenServer.server()) ::
124+
%{
125+
info: non_neg_integer(),
126+
debug: non_neg_integer(),
127+
warn: non_neg_integer(),
128+
error: non_neg_integer()
129+
}
130+
| {:error, term()}
124131
def count_next(client_pid) do
125132
GenServer.call(client_pid, :count_next)
126133
end
@@ -235,11 +242,12 @@ defmodule RingLogger.Client do
235242
end
236243

237244
def handle_call(:count_next, _from, state) do
238-
count =
245+
counts =
239246
Server.get(state.index, 0)
240-
|> Enum.count(&should_print?(&1, state))
247+
|> Enum.filter(&should_print?(&1, state))
248+
|> Enum.frequencies_by(&elem(&1, 0))
241249

242-
{:reply, count, state}
250+
{:reply, counts, state}
243251
end
244252

245253
def handle_call({:tail, n}, _from, state) do

test/ring_logger_test.exs

+16
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,22 @@ defmodule RingLoggerTest do
499499
end
500500
end
501501

502+
describe "count_next/1" do
503+
test "returns per-level log counts for next set of log messages" do
504+
assert RingLogger.count_next() == %{}
505+
Logger.info('foo')
506+
assert RingLogger.count_next() == %{info: 1}
507+
Logger.debug('bar')
508+
assert RingLogger.count_next() == %{info: 1, debug: 1}
509+
Logger.warn('baz')
510+
assert RingLogger.count_next() == %{info: 1, debug: 1, warn: 1}
511+
Logger.error('uhh')
512+
assert RingLogger.count_next() == %{info: 1, debug: 1, warn: 1, error: 1}
513+
Logger.info('foo')
514+
assert RingLogger.count_next() == %{info: 2, debug: 1, warn: 1, error: 1}
515+
end
516+
end
517+
502518
defp capture_log(fun) do
503519
capture_io(:user, fn ->
504520
fun.()

0 commit comments

Comments
 (0)