EctoTrail allows to store changeset changes into a separate audit_log
table.
- Add
ecto_trail
to your list of dependencies inmix.exs
:
def deps do
[{:ecto_trail, "~> 0.2.0"}]
end
- Ensure
ecto_trail
is started before your application:
def application do
[extra_applications: [:ecto_trail]]
end
- Add a migration that creates
audit_log
table topriv/repo/migrations
folder:
defmodule EctoTrail.TestRepo.Migrations.CreateAuditLogTable do
@moduledoc false
use Ecto.Migration
@table_name String.to_atom(Application.fetch_env!(:ecto_trail, :table_name))
def change(table_name \\ @table_name) do
EctoTrailChangeEnum.create_type
create table(table_name) do
add :actor_id, :string, null: false
add :resource, :string, null: false
add :resource_id, :string, null: false
add :changeset, :map, null: false
add(:change_type, :change)
timestamps([type: :utc_datetime, updated_at: false])
end
end
end
- Use
EctoTrail
in your repo:
defmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_app
use EctoTrail
end
- Configure table name which is used to store audit log (in
config.ex
):
config :ecto_trail, table_name: "audit_log", redacted_fields: [:password, :token]
- Use logging functions instead of defaults. See
EctoTrail
module docs.
The docs can be found at https://hexdocs.pm/ecto_trail.