Sebb

Sebb

Logger: build log-message as data, postpone actual logging

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

Marked As Solved

Sebb

Sebb

there is a bug in the last version, it can’t handle complex data, this should be better, but most likely is still flawed.

   # with log level :info

   import PureLogger
    
    assert [{:logger, :error, {4711, %{file: _, line: _}}}] = error(4711)
    assert [{:logger, :error, {'test', %{file: _, line: _}}}] = error('test')
    assert [{:logger, :error, {"test", %{file: _, line: _}}}] = error("test")
    assert [{:logger, :error, {[1, 2, 3], %{file: _, line: _}}}] = error([1, 2, 3])
    assert [{:logger, :error, {[a: 1, b: 2], %{file: _, line: _}}}] = error([a: 1, b: 2])
    assert [{:logger, :error, {{1, 2}, %{file: _, line: _}}}] = error({1, 2})
    assert [{:logger, :error, {{1, 2, 3}, %{file: _, line: _}}}] = error({1, 2, 3})
    assert [{:logger, :error, {%{a: 1}, %{}}}] = error(%{a: 1, b: 2, c: 3})
    assert [] = debug(%{a: 1, b: 2, c: 3})
defmodule PureLogger do
  @levels [:emergency, :alert, :critical, :error, :warning, :notice, :info, :debug]

  for level <- @levels do
    defmacro unquote(level)(data) do
      maybe_log(unquote(level), data, __CALLER__)
    end
  end

  def maybe_log(level, data, caller) do
    meta = Map.take(caller, [:file, :line])

    if do_log?(level) do
      quote do
        [
          {
            :logger,
            unquote(level),
            {
              unquote(data),
              unquote(Macro.escape(meta))
            }
          }
        ]
      end
    else
      []
    end
  end

  defp do_log?(level) do
    min_level = Application.get_env(:toy, :log_min_level, :all)
    :logger.compare_levels(level, min_level) == :gt
  end
end

Also Liked

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
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.

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

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement