Timber Ecto v1.0.0 Timber.Ecto View Source
Timber integration for Ecto.
Timber can hook into Ecto’s logging system to gather information about queries
including the text of the query and the time it took to execute. This
information is then logged as a Timber.Events.SQLQueryEvent
.
To install Timber’s Ecto event collector, you only need to modify the
application configuration on a per-repository basis. Each repository has a
configuration key :loggers
that accepts a list of three element tuples where
each tuple describes a log event consumer. If you do not have a :loggers
key
specified, Ecto uses the default list of [{Ecto.LogEntry, :log, []}]
which
tells the repository to log every event to Ecto.LogEntry.log/1
. In order to
avoid duplicate logging, you will want to make sure it isn’t in the list when
using this event collector.
The tuple for Timber’s event collector is {Timber.Integrations.EctoLogger, :log, []}
. Many applications will have only one repository named Repo
,
which makes adding this easy. For example, to add it to the repository
MyApp.Repo
:
config :my_app, MyApp.Repo,
loggers: [{Timber.Ecto, :log, []}]
By default, queries are logged at the :debug
level. If you want to use a
custom level, simply add it to the list of arguments. For example, to log
every query at the :info
level:
config :my_app, MyApp.Repo,
loggers: [{Timber.Ecto, :log, [:info]}]
Timing
The time reported in the event is the amount of time the query took to execute on the database, as measured by Ecto. It does not include the time that the query spent in the pool’s queue or the time spent decoding the response from the database.
Log only slow queries
If your queries are noisy you can specify a threshold that must be crossed in order for the query to be logged:
config :timber_ecto,
query_time_ms_threshold: 2_000 # 2 seconds
In the above example, only queries that exceed 2 seconds in execution time will be logged.
Link to this section Summary
Functions
Identical to log/2
except that it uses a default level of :debug
Takes an Ecto.LogEntry
struct and logs it as a Timber.Event.SQLQueryEvent
event at the designated level
Link to this section Functions
Identical to log/2
except that it uses a default level of :debug
log(Ecto.LogEntry.t(), Logger.level()) :: Ecto.LogEntry.t()
Takes an Ecto.LogEntry
struct and logs it as a Timber.Event.SQLQueryEvent
event at the designated level
This function is designed to be called from Ecto’s built-in logging system
(see the module’s documentation). It takes an Ecto.LogEntry
entry struct and
parses it into a Timber.Event.SQLQueryEvent
which is then logged at the
designated level.