Using LoggingExtras to filter LogMessages that were generated from a specific function

Hello , LoggingExtras lets the coder filter messages that satisfy a condition.

  1. Is it possible to keep only the messages that were issued from within a given function
  2. Is it possible to combine the above condition with a LogLevel (as per Julia Documentation)?

Thanks!

if you can change the log message itself, one easy way is to add a group, like @info "hi" _group="myfunction". Then you can filter on group and LogLevel with LoggingExtras.EarlyFilteredLogger. Or use a custom field like @info "hi" function="myfunction", which you can access from LoggingExtras.ActiveFilteredLogger.

If you can’t change the log message itself, I think this is still technically possible with enough hacking, but I don’t think it would be very simple. The issue is that the name of the enclosing function is not stored with the log message itself, only the file name and line number. Those which can be accessed from LoggingExtras.ActiveFilteredLogger, but not the function name. So inside the filter, one would need to check the file/line against the source code itself to figure out the enclosing function (probably with some caching or a pregenerated index to prevent parsing of source files on every log invocation). JuliaSyntax.jl is a precise source parser, so one can use that to go from file/line numbers to syntax, and then you’d need to traverse the syntax tree up until the enclosing function.