Why is Logger composed of Macros?

Had to use Logger in one of my applications today, and remembered that I needed to call require first. So I decided to look at the Logger source code to see why debug, info, error, etc. are macros and not simple functions.

From the code, the macros for debug, info, etc (and even their underlying functions) look very simple. Wasn’t it possible to simply export them as methods instead of macros?

Parallel StackOverflow Question:

1 Like

As I recall it is to allow the code to just entirely not exist for logging levels that you do not want, so the code is never even called and incurs no speed penalty, thus you only pay for the logging levels that you are actively logging. :slight_smile:

2 Likes

It’s so the entire Logger call can be stripped out of the code at compile time if you use :compile_time_purge_level.

4 Likes

How much is this actually used in production systems? I am assuming that while you may want to change the logging level at runtime you don’t always have the luxury of being able to recompile and reload code. (though I have seen some smart tricks to work around this)

This of course very much depends on the type of system you are running.

There are different levels of logging, as in there is the actual logged level given to an output (and there can be many outputs, such as a remote server, local file, stdout, whatever), and there is the ‘compiled’ level, of which anything below is stripped out at compile time. That is usually good to strip out especially noisy levels like debug (I keep info and higher in prod).

The answer on stack overflow is correct. It is a macro so it gets metadata about the caller, such as file, line, module, function, app, etc. This metadata may be logged and it may be used in the future for tracing/filtering.

That was the original motivation anyway. The compile time purging came later.

5 Likes