Astarno

Astarno

Using the Interceptor library to log GenServer behavior

Hi there,

I am currently trying to add logging capabilities to GenServers. I am aware that there are existing solutions that provide insights into a GenServer’s behavior, including logging, but this is not what I am looking for. I am looking to implement a solution myself, one I can afterwards customize and expand upon (e.g. instead of logging make API calls). For this purpose, I found the Interceptor library. This seems great for what I want to do as it allows for some custom code could be executed before/after/… a specified function each time it is executed.

What I did until now is the following. First, I made my own module called GenServerViz. In this module I basically copied all of the regular GenServer code I found on GitHub, but then expanded it with the Interceptor library. I then use GenServerViz instead of the regular GenServer. Using this approach I was able to intercept all functions that are explicitly defined in GenServers, such as start, start_link, stop, call, cast, etc. However, I am currently clueless how I would go about intercepting the callbacks such as handle_cast, handle_call, init, etc as these functions are not explicitly defined in GenServers but are usually defined in the user modules themselves.

The ‘ugly’ solution would be to setup the interception for each module that makes use of my GenServerViz. This would include adding import statements, setting up configuration and either annotating or wrapping the relevant functions inside an intercept block. This would be a very cumbersome process on a bigger scale. It would also mean that one cannot easily switch from the regular GenServer to GenServerViz by just changing the use/import statements in existing projects, something I would like to be easily possible.

Any suggestions?

Where Next?

Popular in Questions Top

New
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

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 48342 226
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New

We're in Beta

About us Mission Statement