🌲 Timber integration for Ecto
The Timber Ecto library provides enhanced logging for your Ecto queries.
Installation
Ensure that you have both :timber (version 3.0.0 or later) and :timber_ecto listed
as dependencies in mix.exs:
def deps do
[
{:timber, "~> 3.0"},
{:timber_ecto, "~> 1.0"}
]
end
Then run mix deps.get.
You'll need to add a configuration line for every Ecto Repo. For example, if you
have the application :my_app and the Ecto Repo MyApp.Repo, the configuration
in config/config.exs would look like this:
use Mix.Config
config :my_app, MyApp.Repo,
log: false
You'll also have to attach Timber's Telemetry event handler in the Repo's init callback:
# lib/my_app/repo.ex
def init(_, opts) do
:ok = Telemetry.attach(
"timber-ecto-query-handler",
[:my_app, :repo, :query],
Timber.Ecto,
:handle_event,
[]
)
{:ok, opts}
endFor more information, see the documentation for the Timber.Ecto module.
Notes for Umbrella Applications
When integrating Timber with Ecto for an umbrella application, the
:timber_ecto library needs to be a dependency for every application that
defines an Ecto Repo.
Advanced
Logging SQL queries can be useful but noisy. To reduce the volume of SQL queries you can limit your logging to queries that surpass an execution time threshold:
:ok = Telemetry.attach(
"timber-ecto-query-handler",
[:my_app, :repo, :query],
Timber.Ecto,
:handle_event,
[query_time_ms_threshold: 2_000]
)In the above example, only queries that exceed 2 seconds in execution time will be logged.
License
This project is licensed under the ISC license. See the file LICENSE for the
full text.