erlang-lager / lager

A logging framework for Erlang/OTP
Apache License 2.0
1.12k stars 456 forks source link

How to get log level in dynamic metadata? #569

Open yulius-take2 opened 2 years ago

yulius-take2 commented 2 years ago

I would like to add a stack trace to the metadata only if lager:error() is called. I'm trying to use the dynamic metadata but since the function has zero arguments, I can't tell what level the current log is called with.

https://github.com/erlang-lager/lager#setting-dynamic-metadata-at-compile-time

Is there a function I can call to see what the current log level is or is there a better hook to be called in the same process that lager:error() is called in?

jadeallenx commented 2 years ago

It is way easier to do something like this from your own custom lager backend. Presumably you are capturing a stack trace from a call site. If so, you can ship it using lager:md/1, then in your backend, you can inspect lager_msg:severity(Message) and if it's higher than error, you can pull the stack trace from the metadata and inject it into the message to be emitted by lager.