jeroenbourgois

jeroenbourgois

Ex_activity - web app logging with Elixir and Ecto

Repo:

Docs:
https://hexdocs.pm/ex_activity

This is our first stab at a Elixir library and our very first OSS contribution! We hope it can be of use to anyone, we are using it ourselves in production.

We created the lib because after 10 years of developing PHP apps we had settled with a structured way to gather logs from our apps, in a database. We really liked this setup, so we ported it to use in our Elixir/Phoenix apps.

Usage is pretty straightforward, where you want to log, say in an API UserController, you just write:

ExActivity.log(%ExActivity.Log{
  type: "api_call",
  action: "UserController/list" ,
  result: "user_count: 10"
})

It will write the data to the database in a Task.start_link way so it interferes as little as possible with your our µ-second response times. That way, we can log enough data and still get max performance. The first update that is coming is to make the data attribute accept structs to the log entry.

Feedback is welcome!

Most Liked

Eiji

Eiji

@jeroenbourgois: Here are my ideas:

  1. Add Logger backend, see: “Custom backends” section.
  2. Add controller field.
  3. Fetch controller and action name from conn, see: Phoenix.Controller.action_name/1 and Phoenix.Controller.controller_module/1
  4. Fetch header using: Plug.Conn.get_req_header/2 instead change all headers to map.

Note: You could save controller like:

alias Phoenix.Controller
# ...
controller_module = Controller.controller_module(conn)
"Elixir." <> module_string = Atom.to_string(controller_module)
# so we could do something like:
controller = String.to_existing_atom("Elixir." <> ex_activity_log.controller)
# do something with controller ...

Look at this case>
You could change it in to ways:

case headers[header] do
  nil -> ""
  value -> value # note do not call fetch again!
end
# or even simpler:
headers[header] || "" # because nil || "" returns: ""

For more information see: Kernel.||/2

In this line - you should have full call, please see: Avoid needless pipelines like the plague. code style rule by @lexmag.

Funny, but you could add pipe to another line

Here you have credo style guide and here its library that will look at your code with simple mix task.

Let me know what do you think about it. :slight_smile:

jeroenbourgois

jeroenbourgois

@Eiji thanks for your input! We are very new to the Elixir game so your styleguide comments are very welcome. I will look into it to make the library better!

jeroenbourgois

jeroenbourgois

Just a small reply, your proposals 2, 3 and 4 make a lot of sense. The first one, not so much to is. Here is why: we created the lib just for that specific reason: we did not want to use Logger, we had it at first (logging to a papertrail backend) but while this is great for all the out-of-the-box logs you get with Phoenix, we did not want that for ‘in controller’ logs.

That being said, after looking into the docs for custom backends, it might just be what we wanted in the first place :slight_smile: Do the logs you sent with Logger also happen in a different process, do you know that? If so, it might just be worthwhile to develop it as a custom backend which will make it much more portable and pluggable.

Again: thank you for your input!

PS: just because I am curious, if we were to get more info from the conn that is Phoenix specific, will we have Phoenix as a dependency? Not that it would be a big problem since we are using it in a Phoenix app to start from…

NobbZ

NobbZ

Hello @jeroenbourgois!

Its much easier to get feedback when you provide necessary links to sources and library :wink:

Also it might help to better explain what your app is meant to do and whom you are refering to by “we”.

Where Next?

Popular in Libraries Top

Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
deadtrickster
I’ve just released stable versions of my Prometheus Elixir libs: Elixir client [docs]; Ecto collector [docs]; Plugs instrumenter/Export...
New
benlime
I created a new library GitHub - benvp/ex_cva: Class Variance Authority for Elixir which aims to make it very easy to define different va...
New
Antrater
Hi everyone! I’m thrilled to announce a huge thing. We have been developing Elixir Moon Design System for quite a while. We are finally ...
New
engineeringdept
I’ve just released the first version of Snap, an Elasticsearch client. It borrows ideas about application structure and process managemen...
New
Hal9000
Here is my first stab at this. README pasted below. https://github.com/Hal9000/elixir_random Comments and critiques are welcome. Th...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New
ostinelli
Let’s write a database! Well not really, but I think it’s a little sad that there doesn’t seem to be a simple in-memory distributed KV da...
New
versilov
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Sub Categories:

We're in Beta

About us Mission Statement