How can I see the SQL executed during an Ecto test?
Asked Answered
H

3

8

When running an Ecto test with mix test ..., the SQL executed is not shown. As far as I can tell, it is not logged to any file, either. How can I see it? (I am using Ecto as part of a Phoenix application.)

Hurlbut answered 14/2, 2017 at 21:0 Comment(0)
R
22

Ecto logs the SQL queries with the level :debug. By default, the Logger level is set to :warn in config/test.exs, which will completely ignore :debug level logs. You can lower the level to :debug to see the SQL queries executed by Ecto. In config/test.exs, change:

config :logger, level: :warn

to

config :logger, level: :debug

and then run mix test.

You can also change the level with which Ecto logs the queries by following the :loggers instructions here.

Reachmedown answered 14/2, 2017 at 21:6 Comment(3)
Thank you! I found the logger settings in config/ but somehow didn't spot that it was different in the test environment, so I thought something more complicated was happening...Hurlbut
@dogbert for some reason I still see no queries, any other thoughts as to what I may have to do? Or why queries wouldn't show up? I am working with an umbrella app and have tried running a specific test from the top level, a specific test from within a sub app, the entire suite from the top level, etc etc. I am at a loss of ideas!Huggermugger
Oh, it turns out we were setting ExUnit to capture logs on start, ExUnit.start/1 takes a list of options which can include :capture_log, setting that to false (well that's the default, so just removing the manual setting of it to true) worked. You can also override it on a per test basis with @tag capture_log: falseHuggermugger
H
2

In case anyone else ran into the issue of still not seeing them even after setting the logger level to :debug, it may be that you have configured ExUnit to capture_logs (likely passed to start/1)

Huggermugger answered 31/3, 2021 at 21:38 Comment(0)
B
2

You can also turn on debug level just for one test by placing the following line within a test. This is useful for a quick check.

test "..." do
  Logger.configure(level: :debug)
  #...
end
Bahia answered 20/4, 2022 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.