Ecto.DevLogger
An alternative logger for Ecto queries.
It inlines bindings into the query, so it is easy to copy-paste logged SQL and run it in any IDE for debugging without manual transformation of common elixir terms to string representation (binary UUID, DateTime, Decimal, json, etc). Also, it highlights db time to make slow queries noticeable. Source table and inlined bindings are highlighted as well.
Format queries
It is possible to format queries using a :before_inline_callback
option.
Here is an example of setup using pgFormatter as an external utility:
defmodule MyApp.Application do
def start(_type, _args) do
Ecto.DevLogger.install(MyApp.Repo, before_inline_callback: &__MODULE__.format_sql_query/1)
end
def format_sql_query(query) do
case System.shell("echo $SQL_QUERY | pg_format -", env: [{"SQL_QUERY", query}], stderr_to_stdout: true) do
{formatted_query, 0} -> String.trim_trailing(formatted_query)
_ -> query
end
end
end