Skip to content

Commit 4a6e10d

Browse files
committed
Add TransformerLogger
1 parent 520727a commit 4a6e10d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/transformer.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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

Comments
 (0)