|
| 1 | +""" |
| 2 | + TransformerLogger(f, logger) |
| 3 | +Preprocesses log messages, using the function `f`, before passing them to the |
| 4 | +`logger` that is wrapped. |
| 5 | +This can be used, for example, to truncate a log message. |
| 6 | +to conditionally change the log level of logs from a given module |
| 7 | +(which depending on the wrappped `logger`, might cause the message to be dropped). |
| 8 | +
|
| 9 | +The transforming function `f` is given a named tuple with the fields: |
| 10 | +`(level, message, _module, group, id, file, line, kwargs)` |
| 11 | +and should return the same. |
| 12 | +See `?LoggingExtra.handle_message_args` for more information on what each is. |
| 13 | +""" |
| 14 | +struct TransformerLogger{T<:AbstractLogger, F} <: AbstractLogger |
| 15 | + transform::F |
| 16 | + logger::T |
| 17 | +end |
| 18 | + |
| 19 | + |
| 20 | +function handle_message(transformer::TransformerLogger, args...; kwargs...) |
| 21 | + log_args = handle_message_args(args...; kwargs...) |
| 22 | + new_log_args = transformer.transform(log_args) |
| 23 | + |
| 24 | + args = Tuple(new_log_args)[1:end-1] |
| 25 | + kwargs = new_log_args.kwargs |
| 26 | + |
| 27 | + if comp_handle_message_check(transformer.logger, args...; kwargs...) |
| 28 | + handle_message(transformer.logger, args...; kwargs...) |
| 29 | + end |
| 30 | +end |
| 31 | + |
| 32 | +shouldlog(transformer::TransformerLogger, args...) = true |
| 33 | + |
| 34 | +min_enabled_level(transformer::TransformerLogger) = BelowMinLevel |
| 35 | + |
| 36 | +catch_exceptions(transformer::TransformerLogger) = catch_exceptions(transformer.logger) |
0 commit comments