Skip to content

Commit 520727a

Browse files
committed
Add TransformerLogger
1 parent 26801b9 commit 520727a

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,43 @@ julia> with_logger(throttled_logger) do
190190
This is basically a special case of the early filtered logger,
191191
that just checks if the level of the message is above the level specified when it was created.
192192

193+
## `TransformerLogger`
194+
The transformer logger allows for the modification of log messages.
195+
This modification includes such things as its log level, and content,
196+
and all the other arguments passed to `handle_message`.
197+
198+
When constructing a `TransformerLogger` you pass in a tranformation function,
199+
and a logger to be wrapped.
200+
The transformation function takes a named tuple containing all the log message fields,
201+
and should return a new modified named tuple.
202+
203+
A simple example of its use is truncating messages.
204+
205+
```
206+
julia> using Logging, LoggingExtras
207+
208+
julia> truncating_logger = TransformerLogger(global_logger()) do log
209+
if length(log.message) > 128
210+
short_message = log.message[1:min(end, 125)] * "..."
211+
return merge(log, (;message=short_message))
212+
else
213+
return log
214+
end
215+
end;
216+
217+
julia> with_logger(truncating_logger) do
218+
@info "the truncating logger only truncates long messages"
219+
@info "Like this one that is this is a long and rambling message, it just keeps going and going and going, and it seems like it will never end."
220+
@info "Not like this one, that is is short"
221+
end
222+
[ Info: the truncating logger only truncates long messages
223+
[ Info: Like this one that is this is a long and rambling message, it just keeps going and going and going, and it seems like it wil...
224+
[ Info: Not like this one, that is is short
225+
```
226+
227+
It can also be used to do things such as change the log level of messages from a particular module (see the example below).
228+
229+
193230
# More Examples
194231

195232
## Filter out any overly long messages
@@ -220,3 +257,23 @@ end
220257
global_logger(EarlyFilteredLogger(not_HTTP_message_filter, global_logger()))
221258
```
222259

260+
## Raising HTTP debug level errors to be Info level
261+
262+
```
263+
using LoggingExtras
264+
using Logging
265+
using HTTP
266+
267+
transformer_logger(global_logger()) do log
268+
if log._module == HTTP && log.level=Logging.Debug
269+
# Merge can be used to construct a new NamedTuple
270+
# which effectively is the overwriting of fields of a NamedTuple
271+
return merge(log, (; level=Logging.Info))
272+
else
273+
return log
274+
end
275+
end
276+
277+
global_logger(transformer_logger)
278+
```
279+

src/LoggingExtras.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Base.CoreLogging:
99
handle_message, shouldlog, min_enabled_level, catch_exceptions
1010

1111
export demux_global_logger,
12-
DemuxLogger, FileLogger,
12+
DemuxLogger, TransformerLogger, FileLogger,
1313
ActiveFilteredLogger, EarlyFilteredLogger, MinLevelLogger
1414

1515

@@ -32,7 +32,8 @@ function comp_handle_message_check(logger, args...; kwargs...)
3232
end
3333
###############################
3434

35-
include("demuxlogger.jl")
35+
include("demux.jl")
36+
include("transformer.jl")
3637
include("activefiltered.jl")
3738
include("earlyfiltered.jl")
3839
include("minlevelfiltered.jl")

src/activefiltered.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The `filter` should be a function that returns a boolean.
1717
`true` if the message should be logged and `false` if not.
1818
As input it will be given a named tuple with the following fields:
1919
`(level, message, _module, group, id, file, line, kwargs)`
20-
See `?LoggingExtra.handle_message_ags` for more information on what each is.
20+
See `?LoggingExtra.handle_message_args` for more information on what each is.
2121
"""
2222
struct ActiveFilteredLogger{T <: AbstractLogger, F} <: AbstractLogger
2323
filter::F
File renamed without changes.

test/runtests.jl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using LoggingExtras
22
using Test
33
using Base.CoreLogging
4-
using Base.CoreLogging: Debug, Info, Warn
4+
using Base.CoreLogging: Debug, Info, Warn, Error
55

66
using Test: collect_test_logs, TestLogger
77

@@ -84,3 +84,26 @@ end
8484
end
8585

8686

87+
@testset "Transformer" begin
88+
testlogger = TestLogger(min_level=Error)
89+
transformer_logger = TransformerLogger(testlogger) do log_msg
90+
# We are going to transform all warnings into errors
91+
if log_msg.level == Warn
92+
return merge(log_msg, (; level=Error))
93+
else
94+
return log_msg
95+
end
96+
end
97+
98+
with_logger(transformer_logger) do
99+
@info "info1"
100+
@warn "Yo Dawg! It is a warning"
101+
@info "info2"
102+
@info "Yo Dawg! It's all good"
103+
@info "info 3"
104+
@error "MISTAKES WERE MADE"
105+
end
106+
@test length(testlogger.logs) == 2
107+
end
108+
109+

0 commit comments

Comments
 (0)