jeroenbourgois

jeroenbourgois

Ex_activity - web app logging with Elixir and Ecto

Repo:
http://github.com/jackjoe/ex_activity

Docs:

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…

Where Next?

Popular in Announcing Top

asiniy
Hey there! I wrote a download elixir package which does exactly what its name about - an easy way to download files. I saw solutions ab...
New
ityonemo
Currently just starting out on a new mini-project - getting zig NIFs to run in elixir. https://github.com/ityonemo/zigler The idea here...
New
michalmuskala
Another small library today. PersistentEts Hex: persistent_ets | Hex GitHub: GitHub - michalmuskala/persistent_ets · GitHub Ets table ...
New
Jskalc
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue. It’s a seamless integration of Vue and Phoenix LiveView, i...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
zachdaniel
Ash Framework What is Ash? Ash Framework is a declarative, resource-oriented application development framework for Elixir. A resource can...
New
Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 14327 100
New
marcuslankenau
I feel kind of stuck with the absence of a proper xml library for Elixir. Currently I use SweetXML which was ok for me more or less to pa...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
144 10187 141
New
trisolaran
Hi! :waving_hand: I would like to present LiveSelect, a little library that I wrote to easily add a dynamic selection input to your LV f...
198 10858 107
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement