joaquinalcerro
Audit Logs - Ecto Database
Hi everyone,
I have recently been asked to add some tracking functionality to the changes made in a Ecto Database. At first, I planed to use a Ecto.Multi to wrap the Ecto functions but that will require lots of changes to the code base. Then, I found ex_audit which I tried in a demo environment and seems to be working. My concern about it is replacing the original Repo functions insert, update, delete… not sure how long this will last until a change in Ecto brakes the library which I am not sure if it is been maintained. I also found a post where @josevalim suggests someone to see if the prepare_changes function would work for his use case and I think it might work on my case.
Haven said that and just before I decide which way to go, I will like to ask you guys with more experience how have you solved this issue.
Thanks for you help and recommendations,
Best regards,
Joaquin Alcerro
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










Most Liked
benwilson512
I think the first main question you need to ask yourself is: Are you auditing changes to rows in tables, or are you auditing actions performed by users?
The distinction is this: When auditing users, my experience is that you are better off thinking in terms of logical operations (often at the API / controller layer) that users perform, the parameters that they pass in, and include information like ip address, request id, and authorization information. If you are auditing rows on tables, then yeah you’ll maybe want something Repo or trigger based.
In my experience is that most of the time you want an audit log you’re really trying to audit users, isn’t really well handled by the trigger or Repo based approaches. A single user operation will often touch multiple rows in multiple tables, and as your application develops you may need to use things like
insert_allorupdate_allwhich are more difficult to track from a Repo perspective.jeroenvisser101
I don’t want to speak for José and his team, but in Dashbit’s Bytepack, they included an
AuditLogstruct and plugs, which you may find useful:https://github.com/dashbitco/bytepack_archive/blob/main/apps/bytepack/lib/bytepack/audit_log.ex
and, Hex.pm also has an implementation that is similar but different:
https://github.com/hexpm/hexpm/blob/main/lib/hexpm/accounts/audit_log.ex
We’ve been using an adapted version of Bytepack’s
AuditLogfor I think about a year now, and it’s been working great for us. While we don’t query the data unless needed, Hex.pm’s version also has indexes on the JSONB column to speed that up.kartheek
GitHub - bitcrowd/carbonite: Audit trails for Elixir/PostgreSQL based on triggers · GitHub - came across this sometime ago.
Last Post!
kartheek
There are some solutions built on top of postgres :
I came across pgAudit in google cloud docs and aws docs.
Debezium - https://debezium.io/
Audit Trigger for 9.1+ - Audit trigger 91plus - PostgreSQL wiki
Carbonite mentions Audit Trigger 91 as its inspiration - GitHub - bitcrowd/carbonite: Audit trails for Elixir/PostgreSQL based on triggers · GitHub.
Carbonite has a section in their docs about these - Carbonite — Carbonite v0.16.1