[Proposal] Logger.Lazy

86 views
Skip to first unread message

Michał Muskała

unread,
Aug 3, 2017, 12:17:04 PM8/3/17
to Elixir-lang-core
Hello everybody,

Elixir's logger is a solid library, it has one awkwardness in use, though. When logging data that might be expensive to produce (and arguably anything besides static strings is expensive to produce for logging), logger requires wrapping the content in a function.

This is dictated by the fact that otherwise logger can't guarantee the argument to the logger macros will be evaluated, so it has to be explicitly marked as lazy. Unfortunately, while this solution is correct, it is quite awkward to use and makes the code less readable.

I propose adding a new Logger.Lazy module with the same interface as the Logger module - and by that I mean all the debug, info, warn and error macros. The only difference would be in the fact that macros from that module, wouldn't give guarantee as to the execution of the argument of the log macros. This could be achieved by implicitly wrapping in a function or probably (and preferably) injecting the test condition directly into the call site.

Introduction of a new module makes it extremely easy to use in new code (without needing to learn anything new or exploring new interfaces) and adding a separate module makes it possible to update old code extremely easily - all that's needed is change the line

    require Logger

into

    require Logger.Lazy, as: Logger

Michał.

OvermindDL1

unread,
Aug 3, 2017, 1:39:37 PM8/3/17
to elixir-lang-core
Considering it is a breaking change anyway, why not keep it in `Logger` but just append `_lazy` to the existing functions, so `warn` has a `warn_lazy` and so forth as well?

I'm curious why logger guarantees execution of the argument anyway as almost no logging library in any other language I've used guarantees that (as the entire calls can vanish at compile time depending on settings).  Perhaps it would be good to change the default style in Elixir 2.0 so that there are no argument evaluation guarantees at all and postpone the changes until then (people can just use functions until then)?

Michał Muskała

unread,
Aug 3, 2017, 2:44:59 PM8/3/17
to elixir-lang-core
Adding a new module is not a breaking change. Why would it be?

Michał.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/28ed5856-ed06-47f7-a290-f9ee210e3b48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

OvermindDL1

unread,
Aug 3, 2017, 3:36:42 PM8/3/17
to elixir-lang-core
Er, I did not mean that 'yours' was the breaking change but rather I mean my later proposal of changing the overall style to assume no arguments expressions are executed would be a breaking change, I was a bit backwards there.  Too little coffee...  ^.^;

andrei sura

unread,
Nov 12, 2017, 2:32:27 PM11/12/17
to elixir-lang-core
I think logging is currently very easy do wrong (huge execution slowdown), so I would think that "wrapping into a function " should be implemented even for the current implementation of logger functions.
Reply all
Reply to author
Forward
0 new messages