Optimal way to capture __method__ from #logger? #219
Replies: 1 comment 1 reply
-
This has recently bubbled up as a larger issue for me. Because most filters are closures, filtering doesn't really seem to be able to get the caller's Binding unless passed it as an argument. I'm exploring the possibility of trying to do this in a template, but finding the same issue: when I try to call #caller or #caller_locations I can't get anything further back than the frame in which the Log object is being accessed. Right now, I have a code base where I have to explicitly pass |
Beta Was this translation helpful? Give feedback.
-
When using SemanticLogger::Loggable on a specific project, I'm finding myself having to add
method: __method__
as payload in a lot of calls. While it certainly works to calllogger.info "foo", method: __method__
it seems rather cumbersome to do that all over the place. Named tags would impose scope gates on all calls to #logger, which isn't always desirable if one wants to add the same named tags to a large block of code.I also considered using a filter or formatter, but then I'd have to use Kernel#caller_locations to identify the calling method, which seems like it might not always be
caller_locations(0, 1)
in every case, especially in a REPL like IRB or Pry where the TOPLEVEL_BINDING for main complicates this approach. Including SemanticLogger::LoggableClass::ClassMethods and invoking #logger_measure_method on a list of methods as a callback when including SemanticLogger::LoggableClass seems to work in some cases even in a REPL, but fails on singleton methods or methods defined dynamically after instantiating the instance.I'm also unsure how to access file_name_and_line from the :json formatter as a callback or proc since there doesn't seem to be an obvious way to grab this from anywhere but the :raw template or as defined in SemanticLogger::Log. I know I could inherit from the JSON formatter and add #file_name_and_line to the array of returned items, but I was hoping there was a more dynamic approach.
What would be the most practical way of carrying #logger's calling #method without having to pass it as a payload item, proc, or named tag on each call?
Beta Was this translation helpful? Give feedback.
All reactions