Logging Metadata with macros help

Hello all!

I have a project that I have been working on. I have been using function calls such as logger:info/2, logger:debug/2, etc. but have been wanting to add more metadata info. I don’t understand how to make this work, however. I want to set this in the kernel config. Currently I have this:

  [{kernel,
   [{logger_level, ${LOGLEVEL}},
    {logger,
     [{handler, my_std_h, logger_std_h,
       #{ config => #{ file => "/home/${USER}/log/erlang.log",
                       max_no_bytes => 1024000,
                       max_no_files => 5},
          formatter => {logger_formatter, #{template => [time," [",level,"]: ",msg,"\n"], single_line => false}}}}]}]}
].

To my understanding, I should be able to put another tuple in the list with the logger_metadata atom as well as a map…but the map part is confusing me. If I want, say, mfa and pid, how is this going to work? The mfa and pid are different each time something like ?LOG would be called.

I’m probably not making much sense. So the question is: how can I configure my config file and logger_formatter template to include metadata? I really can’t understand the documentaiton

1 Like

Hi there,

In your example you have the following value for template:

[time," [",level,"]: ",msg,"\n"]

in order to add mfa, line, pid and foo you can do the following:

[time," [",level,"] " mfa, ":" line, " ", pid, "{{", foo, "}}", msg,"\n"]

Then you can do ?LOG_INFO("My log with foo", #{foo => bar})

Obviously adding all that data without labels might end up being a mess, so maybe you want to further develop that template or use something like Flatlog. Even if you don’t like it check how the author is generating the template to understand all the possibilities.

Cheers!

2 Likes

This helped, makes more sense now. Thank you!

1 Like