dmitriid

dmitriid

How to: Only enable Ecto logging on errors?

      {:ecto_sql, "~> 3.4"},
      {:phoenix, "~> 1.5.8"},
      {:phoenix_ecto, "~> 4.1"},
      {:myxql, "~> 0.4.0"},

Currently Ecto is driving me crazy in development by outputting hundreds of lines of [debug] code into the console (running iex -S mix phx.server). [1] If I put log: :error into adapter config, this only makes those same hundreds of lines to output with [error] :slight_smile: [2]

I don’t want to disable ecto logging entirely though. If there’s an error happening, I want Ecto to log the error. I don’t think the docs cover how to do it. Do I somehow configure Telemetry to achieve what I need?

Send help! :slight_smile:

[1]

[2]

Marked As Solved

RudManusachi

RudManusachi

Those logs are just printing “query”. If error happens before query executed - you’ll see the error logged. If error happens after query executed - you’ll see both [deug] QUERY... and the error. You could set log: false and only errors, are supposed to be logged anyway.

Also Liked

03juan

03juan

Just tried my own advice and this unfortunately won’t work! :sob:

Docs say that

  • :function - purges all messages with the “function/arity” Remember that if you want to purge log calls from a dependency, the dependency must be recompiled.

So I first modified my logger config to find the logging function:

config :logger, :console,
  format: "$time\n[$level] $metadata\n$message\n\n",
  metadata: [:mfa, :file, :line]

That resulted in

[debug] mfa=Ecto.Adapters.SQL.log/4 file=lib/ecto/adapters/sql.ex line=938
QUERY OK …

But the ecto_sql library at line 938 invokes Logger.log/3 which warns that

The macros debug/2, info/2, notice/2, warning/2, error/2, critical/2, alert/2, and emergency/2 are preferred over this macro as they can automatically eliminate the call to Logger altogether at compile time if desired (see the documentation for the Logger module).

I added an explicit :debug case option to the log/4 source code in my deps folder and recompiled as instructed with mix deps.compile ecto_sql but it’s still logging?

:debug ->
  IO.puts("SQL debug")
  Logger.debug(
    fn -> log_iodata(measurements, metadata) end,
    ansi_color: sql_color(query_string)
  )

config.exs

config :logger, :console,
  format: "$time\n[$level] $metadata\n$message\n\n",
  metadata: [:mfa, :file, :line],
  compile_time_purge_matching: [
    [module: Ecto.Adapters.SQL, function: "log/4", level_lower_than: :info]
  ]
iex(1)> Repo.all(User)
SQL debug
14:02:05.547
[debug] mfa=Ecto.Adapters.SQL.log/4 file=lib/ecto/adapters/sql.ex line=939 
QUERY OK source="users" ...

Hopefully someone with more experience can provide some insight :pray:

dmitriid

dmitriid

Ok, this works, at least for me, and in my particular case.

Given log: false in the config, I made a deliberate error in a query (idd instead of id):

  def get_page_data(id) do
    Quire.Repo.one(from(p in Page, where: p.idd == ^id, select: p))
  end

Ecto failed and printed the following error:

[error] #PID<0.1021.0> running QuireWeb.Endpoint (connection #PID<0.967.0>, stream id 8) terminated
Server: localhost:4000 (http)
Request: GET /admin/pages/33
** (exit) an exception was raised:
    ** (Ecto.QueryError) lib/quire/cms.ex:186: field `idd` in `where` does not exist in schema Quire.CMS.Page in query:

from p0 in Quire.CMS.Page,
  where: p0.idd == ^33,
  select: p0

No other debug logs. At this point this probably covers 99.999% of my needs.

@KristerV Give it a try. Quite possible if it wasn’t working for you it was a bug that was solved at one point.

John-Goff

John-Goff

You should set your log level in your dev.exs file. See Logger — Logger v1.12.3 and Logger — Logger v1.12.3 for more info.

Last Post!

dmitriid

dmitriid

Ok, this works, at least for me, and in my particular case.

Given log: false in the config, I made a deliberate error in a query (idd instead of id):

  def get_page_data(id) do
    Quire.Repo.one(from(p in Page, where: p.idd == ^id, select: p))
  end

Ecto failed and printed the following error:

[error] #PID<0.1021.0> running QuireWeb.Endpoint (connection #PID<0.967.0>, stream id 8) terminated
Server: localhost:4000 (http)
Request: GET /admin/pages/33
** (exit) an exception was raised:
    ** (Ecto.QueryError) lib/quire/cms.ex:186: field `idd` in `where` does not exist in schema Quire.CMS.Page in query:

from p0 in Quire.CMS.Page,
  where: p0.idd == ^33,
  select: p0

No other debug logs. At this point this probably covers 99.999% of my needs.

@KristerV Give it a try. Quite possible if it wasn’t working for you it was a bug that was solved at one point.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement