wfgilman
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
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
josevalim
Can you please confirm how you are calling Logger.info? The API used in your example above is invalid and will raise an ArgumentError.
wfgilman
Oops, that was a type, now fixed. The test runs with the following result:
josevalim
Can you please provide a sample application that reproduces the error? Because for all intents and purposes it should just work.
wfgilman
Sure thing! Here’s my sample application: GitHub - wfgilman/log_capture_test: Example App Testing ExUnit.CaptureLog capture_log/2 assertion. · GitHub
There’s two tests to demonstrate: one for
warnand one forinfo. The first passes and the other does not.OvermindDL1
@wfgilman Have you looked at: log_capture_test/config/test.exs at master · wfgilman/log_capture_test · GitHub
And considering the test that is not passing runs this code: log_capture_test/lib/log_capture/rate_limit.ex at master · wfgilman/log_capture_test · GitHub
It will not print anything, hence why it does not pass. ^.^
You need to adjust the test config to be
:infoinstead of:warnor so.wfgilman
Ah, yes I overlooked that. I updated the config and that test passed. Thanks for spotting that. By default,
capture_log/2is supposed to capture all logs, but I need to make sure the application is emitting all logs for that to workOvermindDL1
Lol, I’ve hit the exact same thing before. It is ‘interesting’ since the config completely eliminates the calls altogether once compiled.
jc00ke
I had this exact issue, changing to
level: :infoworked wonders.However, now my test output is really noisy. Any way to get the benefits of
warnwith respect to test output but still able to captureinfo?Am I reading this incorrectly? elixir/lib/ex_unit/lib/ex_unit/capture_log.ex at v1.7.4 · elixir-lang/elixir · GitHub
I would expect that, even if
config :logger, level: :warnis set, thatwould still capture, though that would seemingly contradict
eteeselink
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
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:
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.I found it all to be documented pretty well in the Logger docs btw: Logger — Logger v1.20.2
jc00ke
Best. Necropost. Ever.
I must have missed it, so thank you for stating the obvious!
My logs are now much less noisy