26

Whatever in iex> or using mix run -e "My.code" when I run the mix project using ecto, the Ecto's Debugging Mechanism display a bunch of SQLs like below

16:42:12.870 [debug] SELECT a0.`id` FROM `account` AS a0 WHERE (a0.`account_name` = ?) ["71000000313"] (39.6ms)`
...

When I dont need the debug output anymore, How can I turn it off, I cannot find anything about how to change ecto log level stuff.

Thanks in advance.

Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
王志軍
  • 1,021
  • 1
  • 11
  • 21

5 Answers5

37

If you want to change the Ecto (pre 2.0) log level (and only it) then you can use the log_level configuration option that can be set in your applications Ecto repository configuration. In example:

config :my_app, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  database: "my_app",
  username: "my_app",
  password: "secret",
  hostname: "localhost",
  port: 5433,
  log_level: :info

Of course beside the above you can always change the Logger configuration log level option if you want to change the overall log level (not only the Ecto log level) e.g.:

config :logger, level: :info

Update (by @Milos):

Since Ecto 2.0.0, instead of log_level: :info you need to use loggers: [{Ecto.LogEntry, :log, [:info]}].

Update (by @AndyMacKinlay):

Since Ecto 3.0.0, instead of log_level: :info you need to use log: :info.

Update (by @Simon):

Since Ecto 3.0.0, you can also completely disable logging log: false.

Szymon Jeż
  • 8,273
  • 4
  • 42
  • 60
  • 6
    This setting was changed in Ecto 2.0.0, instead of `log_level: :info` you need to use `loggers: [{Ecto.LogEntry, :log, [:info]}]`. – Milos Feb 24 '16 at 18:18
  • Milos, thanks. Can you make a new top-level answer for this? – hayesgm May 01 '16 at 21:40
  • 5
    You can also just stop Ecto from logging by setting the value of `loggers` to an empty list, as in `loggers: []`. – Anthony Eden Jan 03 '17 at 08:45
  • 2
    You need to use `log: :info` [in Ecto 3.0](https://hexdocs.pm/ecto/3.0.7/Ecto.Repo.html#content) – Andy MacKinlay Mar 27 '19 at 00:51
  • I'm using Ecto 3.6 and apparently the new option is `:log: false` (this thread still shows high on Google searches). See the docs here https://hexdocs.pm/ecto/Ecto.Repo.html#content – Simon Apr 10 '21 at 12:48
23

Your logging level is configured in your config/#{env}.exs files. If you look into config/prod.exs it most likely already has that level set to :info:

config :logger, level: :info

So if you run the app with MIX_ENV=prod iex -S mix you won't get the debug output. This means that when you build a release with something like MIX_ENV=prod mix release the resulting build won't be producing this output. Alternatively you may set level: :info or :warn for whatever environment you want by changing the appropriate config/#{env}.exs.

Paweł Obrok
  • 22,568
  • 8
  • 74
  • 70
7

To disable debug messages temporarily you can do Logger.configure(level: :warn) and then re-enable with Logger.configure(level: :debug).

https://hexdocs.pm/logger/Logger.html#Levels

alvitawa
  • 394
  • 1
  • 4
  • 12
6

Ecto 3 answer to completely disabling logging would be:

config :app, App.Repo,
  username: "postgres",
  password: "postgres",
  database: "app_dev",
  log: false
Nema Ga
  • 2,450
  • 4
  • 26
  • 49
4

Simple put loggers: [] in

config :my_app, MyApp.Repo, adapter: Ecto.Adapters.Postgres, database: "my_app_repo", username: "DB_USERNAME", password: "DB_PASSWORD", hostname: "DB_HOST", loggers: []

Zubair Nabi
  • 1,016
  • 7
  • 28