Including file name, function arity etc. in logger's output

Hi folks :wave:

I’m working on a project where we want to replace lager with OTP’s logger and there is one thing that’s causing us quite a few headaches.
Since lager uses parse-trasnforms, it can easily include module names, function names, line and column numbers, etc… in each log line, e.g. …

lager:debug("YOUR MESSAGE"),

ends up as…

2024-04-10 13:31:21.440 UTC [debug] <0.3338.0>@your_module:your_function:{26,5} YOUR MESSAGE

Now, we can manually do something like that with logger

logger:debug(
  "YOUR MESSAGE",
  [],
  #{mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY},
    line => ?LINE
  }).

But… we don’t want to repeat that map everywhere. We would also prefer not to put that map in a macro and repeat that macro everywhere. And we would also like not to use a parse transform for this.

Is there any other alternative to achieve the desired result that we’re not considering?

Thanks in advance.

I’d say use the defined ?LOG_* macros :slight_smile:
https://www.erlang.org/doc/man/logger#macros
https://www.erlang.org/doc/man/logger#type-metadata

3 Likes

Indeed - we are also using the LOG_.... macros, and set some extra metadata for logger in controllers and processes using logger:set_process_metadata/1

2 Likes

But that must be done per-process. You can do that globally by setting primary metadata either in kernel config or by calling set_primary_config/1.

1 Like

Thank you, folks!!

All amazing pointers, hands down!

I didn’t know about the ?LOG_* macros. I think they’re the way to go for us, indeed.