Sebb

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

Showing Posts 1 to 10

hauleth

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

Sebb OP

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

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

Ljzn

If you just want to test logging, can use CaptureLog:

  test "capture error log" do
    import ExUnit.CaptureLog
    require Logger
    assert capture_log(fn -> Logger.error("?") end) =~ "?"
  end
Sebb

Sebb OP

I didn’t know about capture_log, nice.
As it seems Logger supports structured logging (you can log a map) so if capture_log can 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

Ljzn

The capture_log only 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:

  test "capture error log" do
    import ExUnit.CaptureLog
    require Logger
    assert capture_log(fn -> Logger.error(%{key: "value"}) end) =~ "key: \"value\""
  end
hauleth

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

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:

defmodule CaptureStructuredLogs do
  def capture_log(callback) do
    this = self()
    handler_id = :"handler_#{inspect(this)}"

    :ok = :logger.add_handler(handler_id, __MODULE__, %{config: %{pid: this}})
    try do
      callback.()
    after
      :logger.remove_handler(handler_id)
    end

    fetch_all_messages([])
  end

  defp fetch_all_messages(messages) do
    receive do
      {{__MODULE__, :log}, log_event} ->
        fetch_all_messages([log_event | messages])
    after
      0 -> Enum.reverse(messages)
    end
  end

  def adding_handler(%{config: %{pid: pid}} = config) when is_pid(pid), do: {:ok, config}
  def adding_handler(_), do: {:error, :required_pid}

  def log(log_event, %{config: %{pid: pid}}) do
    send(pid, {{__MODULE__, :log}, log_event)
  end
end

Now you can use it like “old” function:

test "capture log" do
  import CaptureStructuredLogs
  require Logger
  assert [%{msg: {:report, %{key: "value"}}}] = capture_log(fn -> Logger.error(%{key: "value"}) end)
end
sasajuric

sasajuric

Author of Elixir In Action

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

Sebb OP

That’s cool. With some unpacking in fetch_all_messages:

defp fetch_all_messages(messages) do
  receive do
    {{__MODULE__, :log}, %{level: level, msg: {_, msg}}} ->
      fetch_all_messages([{level, msg} | messages])
...

I can write very clean assertions against the Logger:

assert [info: "this is a info message", error: "this is an error message"] =
          capture_log(fn ->
            Logger.info("this is a info message")
            Logger.error("this is an error message")
          end)

assert [info: %{something: :reported}] =
          capture_log(fn -> Logger.info(%{something: :reported, this: :info}) end)

assert [info: [something: :reported, this: :info]] =
          capture_log(fn -> Logger.info(something: :reported, this: :info) end)

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 the test_helper may be a little intimidating.

@sasajuric I will go for __ENV__ in the core to keep it perfectly immaculate!

Where Next? Top

Trending in Questions Top

katta
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
achenet
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
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
unaware8150
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
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
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 Top

GenericJam
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
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
KristerV
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
juhalehtonen
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews