I’m testing my rate limiting plug and I want to make sure the plug logs an rate limit violations. However, I want this log to be at level :info. The assertion works when I log it at level :error or :warn, but not at :info. I tried setting the options in capture_log/2 but it didn’t have any effect. Am I missing something?
Test
test "rate_limit/2 puts 429 status if rate limit is exceeded", %{conn: conn, opts: opts} do
breach = opts[:max_requests] + 1
for req <- 1..breach do
if req == breach do
assert capture_log([level: :info], fn ->
conn = Api.RateLimit.rate_limit(conn, opts)
assert conn.status == 429
end) =~ "Rate limit violation for bucket"
else
Api.RateLimit.rate_limit(conn, opts)
end
end
end
Function
def rate_limit(conn, opts) do
case check_rate(conn, opts) do
{:ok, _count} ->
conn
{:error, _count} ->
Logger.info(fn ->
bucket = opts[:bucket_name] || default_bucket_name(conn)
"Rate limit violation for bucket: #{inspect bucket}"
end)
render_error(conn)
end
end
Ah, yes I overlooked that. I updated the config and that test passed. Thanks for spotting that. By default, capture_log/2 is supposed to capture all logs, but I need to make sure the application is emitting all logs for that to work
It is possible to configure the level to capture with :level , which will set the capturing level for the duration of the capture, for instance, if the log level is set to :error any message with the lower level will be ignored. The default level is nil , which will capture all messages. The behaviour is undetermined if async tests change Logger level.
I would expect that, even if config :logger, level: :warn is set, that
would still capture, though that would seemingly contradict
The Logger.info/2 macro emits the provided message at the :info level. Note the arguments given to info/2 will only be evaluated if a message is logged. For instance, if the Logger level is set to :warn , :info messages are never logged and therefore the arguments given above won’t even be executed.
At the risk of terrible necroposting and of stating the obvious: I ran into this same problem and saw that the OP’s last remarks went unanswered. I found a solution, so maybe other googlers are helped by this:
Our test.exs had
config :logger,
level: :warn
and only by setting it to :info I could get my capture_log assertions to capture the relevant logs.
However, it totally polluted the test output. As described, setting the log level to :warn actually removes the :debug and :info level log statements from the compiled code entirely.
Fortunately, there’s a way around this:
# capture all logs...
config :logger,
level: :debug
# ... but show only warnings and up on the console
config :logger, :console,
level: :warn
This works because Logger’s default backend, console, can be separately configured to filter log levels. That way the Logger.info calls etc are not removed from the code, but the console output is still sane.