|
| 1 | +""" |
| 2 | + ActiveFilteredLogger(filter, logger) |
| 3 | +
|
| 4 | +Wraps `logger` in an active filter. |
| 5 | +While loggers intrinsictally have in built filtering mechanisms. |
| 6 | +Wrapping it in a `ActiveFilterLogger` allows for extract control, |
| 7 | +at the cost of a bit of overhead. |
| 8 | +
|
| 9 | +The `ActiveFilteredLogger` has full control of what is logged, |
| 10 | +as it sees the full message, |
| 11 | +this does mean however it determines what to log at runtime, |
| 12 | +which is the source of the overhead. |
| 13 | +The [`EarlyFilteredLogger`](@ref) has less control, |
| 14 | +but decides if to log before the message is computed. |
| 15 | +
|
| 16 | +The `filter` should be a function that returns a boolean. |
| 17 | +`true` if the message should be logged and `false` if not. |
| 18 | +As input it will be given a named tuple with the following fields: |
| 19 | +`(level, message, _module, group, id, file, line, kwargs)` |
| 20 | +See `?LoggingExtra.HandleMessageArgs` for more information on what each is. |
| 21 | +""" |
| 22 | +struct ActiveFilteredLogger{T <: AbstractLogger, F} <: AbstractLogger |
| 23 | + filter::F |
| 24 | + logger::T |
| 25 | +end |
| 26 | + |
| 27 | + |
| 28 | +function handle_message(logger::ActiveFilteredLogger, args...; kwargs...) |
| 29 | + log_args = HandleMessageArgs(args...; kwargs...) |
| 30 | + if logger.filter(log_args) |
| 31 | + handle_message(logger.logger, args...; kwargs...) |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +# As an optimisation, we query if the logger we are sending this to will accept |
| 36 | +# this log. If not then there is no point in us taking it |
| 37 | +function shouldlog(logger::ActiveFilteredLogger, args...) |
| 38 | + return shouldlog(logger.logger, args...) |
| 39 | +end |
| 40 | + |
| 41 | +min_enabled_level(logger::ActiveFilteredLogger) = min_enabled_level(logger.logger) |
| 42 | +catch_exceptions(logger::ActiveFilteredLogger) = catch_exceptions(logger.logger) |
| 43 | + |
| 44 | +""" |
| 45 | + HandleMessageArgs |
| 46 | +
|
| 47 | +This is an alias for a NamedTuple containing all the arguments the logger gives |
| 48 | +to `handle_message` |
| 49 | +It is the type pased to the active logger filter. |
| 50 | +These argument come from the logging macro (@info`, `@warn` etc). |
| 51 | +
|
| 52 | + * `level::LogLevel` Warn, Info, etc, |
| 53 | + * `message::String` the message to be logged |
| 54 | + * `_module::Module` can be used to specify a different originating module from |
| 55 | + the source location of the message. |
| 56 | + * `group::Symbol` can be used to override the message group (this is |
| 57 | + normally derived from the base name of the source file). |
| 58 | + * `id::Symbol` can be used to override the automatically generated unique |
| 59 | + message identifier. This is useful if you need to very closely associate |
| 60 | + messages generated on different source lines. |
| 61 | + * `file::String` and `line::Int` can be used to override the apparent |
| 62 | + source location of a log message. |
| 63 | + * `kwargs...`: Any keyword or position arguments passed to the logging macro |
| 64 | +""" |
| 65 | +function HandleMessageArgs(args...; kwargs...) |
| 66 | + fieldnames = (:level, :message, :_module, :group, :id, :file, :line, :kwargs) |
| 67 | + fieldvals = (args..., kwargs) |
| 68 | + return NamedTuple{fieldnames, typeof(fieldvals)}(fieldvals) |
| 69 | +end |
| 70 | + |
| 71 | + |
0 commit comments