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

hauleth

hauleth

Because of the backward compatibility. I can check out if that could be “fixed” by adding new module to ExUnit though. I will try to provide PR tomorrow (CET) unless you will do that first. You can ping me in PR if you want.

Where Next?

Popular in Questions Top

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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

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...
3268 119930 1237
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement