2

is it possible in Elixir to override the Logger functions to always prepend the module where it is being called? "[#{__MODULE__}] "

like

Logger.debug("Fetching Exchange Information and Trading Rules...")

becomes in log:

[debug] [Elixir.Binance.Futures.Rest.Client] Requesting Exchange Information and Trading Rules...

1 Answer 1

5

The requested functionality is already there. Just configure Logger to output the respective metadata (:mfa) as described in the documentation.

config :logger, :console,
  format: "[$level] [$metadata] $message\n",
  metadata: [:error_code, :mfa]
6
  • thanks, any way to see just the module name and not the mfa= part? mfa=Binance.Futures.Rest.Client.get_continuous_klines/4
    – John Smith
    Commented Apr 13, 2021 at 14:29
  • Not that I am aware of. Commented Apr 13, 2021 at 14:41
  • @AlekseiMatiushkin there is a way, but it is soft-deprecated one. You can use :module as an metadata key to fetch just module name. It became soft-deprecated (and thus removed from docs) on 1.10.
    – Hauleth
    Commented Apr 13, 2021 at 19:38
  • 1
    @Hauleth I understood the question as “can I get rid of mfa= prefix in the metadata output,” in a case of using :module, the prefix (module= in this case) would stay. Commented Apr 14, 2021 at 4:16
  • is there a way to do it only per operation? (and not for all logging on my app)
    – bermick
    Commented Jul 8, 2021 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.