|
| 1 | + |
| 2 | +""" |
| 3 | + FilteredLogger(filter, logger) |
| 4 | + |
| 5 | +Wraps `logger` in a filter. |
| 6 | +While loggers intrinsictally have in built filtering mechanisms. |
| 7 | +Wrapping it in a FilterLogger allows for extract control, |
| 8 | +at the cost of a bit of overhead. |
| 9 | +
|
| 10 | +The `filter` should be a function that returns a boolean. |
| 11 | +`true` if the message should be logged and `false` if not. |
| 12 | +It should take as inputs: |
| 13 | +`(level, message, _module, group, id, file, line; kwargs...)` |
| 14 | +
|
| 15 | +These argument come from the logging macro (@info`, `@warn` etc). |
| 16 | + |
| 17 | + * `level::LogLevel` Warn, Info, etc, |
| 18 | + * `message::String` the message to be logged |
| 19 | + * `_module::Module` can be used to specify a different originating module from |
| 20 | + the source location of the message. |
| 21 | + * `group::Symbol` can be used to override the message group (this is |
| 22 | + normally derived from the base name of the source file). |
| 23 | + * `id::Symbol` can be used to override the automatically generated unique |
| 24 | + message identifier. This is useful if you need to very closely associate |
| 25 | + messages generated on different source lines. |
| 26 | + * `file::String` and `line::Int` can be used to override the apparent |
| 27 | + source location of a log message. |
| 28 | + * `kwargs...` any keyword or positional arguments to the logging macro. |
| 29 | +""" |
| 30 | +struct FilteredLogger{T <: AbstractLogger, F} <: AbstractLogger |
| 31 | + filter::F |
| 32 | + logger::T |
| 33 | +end |
| 34 | + |
| 35 | + |
| 36 | +function handle_message(filteredlogger::FilteredLogger, level, message, _module, group, id, file, line; kwargs...) |
| 37 | + if filteredlogger.filter(level, message, _module, group, id, file, line; kwargs...) |
| 38 | + handle_message(filteredlogger.logger, level, message, _module, group, id, file, line; kwargs...) |
| 39 | + end |
| 40 | +end |
| 41 | + |
| 42 | +shouldlog(filteredlogger::FilteredLogger, level, _module, group, id) = shouldlog(filteredlogger.logger, level, _module, group, id) |
| 43 | +min_enabled_level(filteredlogger::FilteredLogger) = min_enabled_level(filteredlogger.logger) |
| 44 | +catch_exceptions(filteredlogger::FilteredLogger) = catch_exceptions(filteredlogger.logger) |
0 commit comments