'How can I see the SQL executed during an Ecto test?

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.)



Solution 1:[1]

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.

Solution 2:[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)

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dogbert
Solution 2 mackshkatz
Solution 3 Andrew