An alternative logger for Ecto queries that uses Logbook.
This package aims at making Ecto logs readable when used with a logfmt formatter.
Read the docs at https://voismart.github.io/ecto-logbook/.
Features:
- Uses log metadata that can be parsed if using a logfmt/json formatter.
- Uses Logbook for logging, making it easy to enable/disable logging
- With
:colorizeoption, Highlights db time for slow queries - With
:inline_paramsoption, tries to inline parameters into the query - If repo
:stacktraceis enabled, MFA information will be included in the log metadata
The base of this project is a fork of https://github.com/fuelen/ecto_dev_logger Differently from the original project, this package doesn't try hard to print valid queries. Instead it just aims for readability of the logs.
The package can be installed by adding ecto_logbook to your list of dependencies in mix.exs:
def deps do
[
{:ecto_logbook, git: "https://github.com/voismart/ecto_logbook.git", tag: "v1.14.3"}
]
endConfigure your logger in config.exs:
import Config
# EctoLogbook will log at debug level
config :logger, :level, :debug
config :logbook, :default_tag_level, :debug
# Use a logfmt formatter and display all metadata
config :logger, :default_formatter, format: {Logbook.Formatters.Logfmt, :format}, metadata: :allIn order to use EctoLogbook, you should disable youe Repo default logs.
config :my_app, MyApp.Repo, log: falseThen install telemetry handler in MyApp.Application:
EctoLogbook.install(MyApp.Repo)- Telemetry handler will be installed _only_ if `log` configuration value is set to `false`.You can then finetune the logging level for EctoLogbook using Logbook.set_level/2:
# Disable logging for EctoLogbook
Logbook.set_level(:ecto_logbook, :none)
# Enable logging for EctoLogbook
Logbook.set_level(:ecto_logbook, :debug)For more configuration options, see Logbook.
By default EctoLogbook will try to do as little transformation as possible to make logging fast.
You can pass options to EctoLogbook.install/2 to fine tune the display behavior:
EctoLogbook.install(MyApp.Repo,
colorize: true, # Use colors to highlight queries, parameters and db time
inline_params: true, # Try to inline parameters in the query
log_repo_name: true, # Log repo name (useful if multiple repos are used)
preprocess_metadata_callback: fn metadata -> metadata end,
ignore_event_callback: fn metadata -> !String.match(metadata[:query], ~r/SELECT/) end
)See EctoLogbook.install/2 for the full doc.
If the repo :stacktrace configuration is set to true, MFA information will be included in the log metadata.
config :my_app, MyApp.Repo, log: false, stacktrace: true