Sebb
I’m just trying to make the functional core of my application as pure as possible, following the functional core, imperative shell pattern.
One problem I have is logging. Consider this code deep in the core:
case var do
:foo ->
{:ok, "foo"}
:bar ->
{:error, "bar"}
end
So one of the possible return values of the core would be{:error, "bar"}. This error is just data, so I can easily write a test, that’s good. But I would also like to log it. I could:
- write a handler in the shell that matches on all errors and logs them. But then I’m missing Logger’s metadata.
- I could prepend each
{:error, ...}with a call to Logger
What I’d rather do is sth like this:
- core:
{:error, {"bar", Logger.get_all_meta([custom_meta: :my_metadata])}} - shell: use this data to log at one place outside the core with correct metadata
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
I would just slap logging in place. While this mean that there is a little of “impurity” in “core” that doesn’t really matter, as that “impurity” do not affect the overall system. So unless there are other considerations that you do not listed in your post, then I would just do not care about it.
However if you want to have it just in case when you need debugging, then you probably would prefer setup dynamic tracing on that function.
Sebb
I number (correct) logging among the overall system.
But the main point is: I’d like to be able to do it for the sake of purity alone. I know that’s not a good reason, but right now I see it as a sport.
I did not yet look into tracing yet, but I definitely will.
hauleth
By “overall system” I meant core computation of your application.
Just remember that there are 2 “kinds” of tracing. Dynamic that should be used only for debugging (as there can be only one dynamic tracer per module) and “static” one that is meant to be observability tool (see OpenTelemetry).
If it is just experimenting then you are free to go, but for me it seems like over complicating things that are meant to be simple.
Ljzn
If you just want to test logging, can use
CaptureLog:Sebb
I didn’t know about
capture_log, nice.As it seems
Loggersupports structured logging (you can log a map) so ifcapture_logcan match on a map (?) the testing-log-messages thing is covered.@hauleth yes just experimenting. I did OOP and C my whole life. Now seeing how things can be done functionally I kind of want to wash all the impurity away. It’s not rational.
Ljzn
The
capture_logonly works with console backend of logger. And the map log will be formated into string when it goes to IO.So you can test it by:
hauleth
No, as @Ljzn said it works with already formatted data (however in contrast to what they said, you aren’t forced to use console logger in test to make it work).
If you want to test against structure of the logged message when working with structured logging then you need a little more footwork. I will provide you example how to do so a little bit later as I am on mobile, so if you could please ping me about it in an hour or two.
hauleth
Ok, now I can write about it.
To be able to listen to the data for given process, then you need to instantiate your own handler. Simplest approach to that would be:
Now you can use it like “old” function:
sasajuric
If I understand correctly, you want to log the file/line where the error is produced? I typically don’t obsess about that, but instead aim to make different branches return different errors, which means that I can deduce the source of the error from its content.
However, if I really wanted to preserve the stack trace I’d probably log in place, as @hauleth said.
But, if you want to stick to pure functional for fun & sport, you could use __ENV__ to include file & line in the returned error, and then include that info in the logged message.
Sebb
That’s cool. With some unpacking in
fetch_all_messages:I can write very clean assertions against the Logger:
Why is something like this not the default for
capture_log? You are explicitly allowed to log maps and keyword lists, why can’t ExUnit match on them (by default). Having this module in thetest_helpermay be a little intimidating.@sasajuric I will go for
__ENV__in the core to keep it perfectly immaculate!