tompave

tompave

Logger: when to use an anonymous function

I am looking for some extra info and examples on when to use the Logger with functions instead of binaries. I’ve read that the function version is lazily evaluated and useful when the log statement could be ignored.

For example, Dave Thomas writes in Programming Elixir 1.2 (chapter 13):

The basic logging functions are Logger.debug, .info, .warn, and .error. Each function takes either a string or a zero-arity function:

Logger.debug "Order total #{total(order)}"
Logger.debug fn -> "Order total #{total(order)}" end

Why have the function version? Perhaps the calculation of the order total is expensive. In the first version, we’ll always call it to interpolate the value into our string, even if the runtime log level is set to ignore debug-level messages. In the function variant, though, the total function will be invoked only if the log message is needed.

This seems to contradict other things I’ve learned as I’m getting more familiar with the language.
For example I’m aware that Logger.info/2 and friends are implemented as macros and the fact that, even though originally it was to get the caller metadata, this allows to remove logging calls that would not be used with the current log level (depending on the value compile_time_purge_level).

Now, since the interface is already made of macros, the binary messages should be lazily evaluated. That is, if Logger.info/2 was a function, the arguments would be evaluated first. Since it’s a macro that returns a quoted expression, the argument will be evaluated later. More precisely, when we hand it over to the Logger.bare_log/3 function.

What’s the purpose of using a function, then? Is it to keep the evaluation lazy even when we change the log level at runtime? How often does that happen though? I’d expect that the expensive log calls will have been already purged through compile_time_purge_level.

Most Liked

a_lixer

a_lixer

This is an old thread, but I think I can close the loop by mentioning that Elixir 1.7 removed the need to use anonymous functions with Logger.

See Logger compile-time purging in Elixir 1.7 release notes.

Also, there is a credo check specifically for this, which is disabled for Elixir 1.7+.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Since it’s a macro that returns a quoted expression, the argument will be evaluated later.

This is not correct. The only thing that being a macro does is give Logger the opportunity to purge at compile time. If you do not do that then it leaves the AST largely alone, and then at runtime it’s evaluated just as you would expect.

Logger COULD always wrap the argument in an anonymous function so that it was always lazy, but this could produce quite a bit of unexpected behaviour for people, so it is not what is done

Plenty of people forego compile_time_purge_level because they want to be able to alter the log level dynamically. If the debug values are compile time purged then even if you change the log level while it’s running the debug calls won’t happen. If you wrap expensive debug calls in an anonymous function, then you can switch to debug logging and get the values dynamically

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

What you have is exactly what happens with compile time purge on, but is not what happens when compile time purge is off. Your check of the log level happens at compile time which makes it impossible to change dynamically at runtime.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement