Skip to content

Commit c86ca31

Browse files
committed
Support function-like objects in FormatLogger
1 parent e215be9 commit c86ca31

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/Sinks/formatlogger.jl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

22
struct FormatLogger <: AbstractLogger
3-
f::Function
3+
formatter
44
stream::IO
55
always_flush::Bool
66
end
77

88
"""
9-
FormatLogger(f::Function, io::IO=stderr; always_flush=true)
9+
FormatLogger(formatter, io::IO=stderr; always_flush=true)
1010
1111
Logger sink that formats the message and finally writes to `io`.
12-
The formatting function should be of the form `f(io::IOContext, log_args::NamedTuple)`
12+
The formatting function should be of the form `formatter(io::IOContext, log_args::NamedTuple)`
1313
where `log_args` has the following fields:
1414
`(level, message, _module, group, id, file, line, kwargs)`.
1515
See [`LoggingExtras.handle_message_args`](@ref) for more information on what field is.
@@ -30,22 +30,22 @@ Main | [Info] This is an informational message.
3030
Main | [Warn] This is a warning, should take a look.
3131
```
3232
"""
33-
function FormatLogger(f::Function, io::IO=stderr; always_flush=true)
34-
return FormatLogger(f, io, always_flush)
33+
function FormatLogger(formatter, io::IO=stderr; always_flush=true)
34+
return FormatLogger(formatter, io, always_flush)
3535
end
3636

3737
"""
38-
FormatLogger(f::Function, path::AbstractString; append=false, always_flush=true)
38+
FormatLogger(formatter, path::AbstractString; append=false, always_flush=true)
3939
4040
Logger sink that formats the message and writes it to the file at `path`. This is similar
4141
to `FileLogger` except that it allows specifying the printing format.
4242
4343
To append to the file (rather than truncating the file first), use `append=true`.
4444
If `always_flush=true` the stream is flushed after every handled log message.
4545
"""
46-
function FormatLogger(f::Function, path::AbstractString; append::Bool=false, kw...)
46+
function FormatLogger(formatter, path::AbstractString; append::Bool=false, kw...)
4747
io = open(path, append ? "a" : "w")
48-
return FormatLogger(f, io; kw...)
48+
return FormatLogger(formatter, io; kw...)
4949
end
5050

5151
function handle_message(logger::FormatLogger, args...; kwargs...)
@@ -54,11 +54,20 @@ function handle_message(logger::FormatLogger, args...; kwargs...)
5454
# to make sure that everything writes to the logger io in one go.
5555
iob = IOBuffer()
5656
ioc = IOContext(iob, logger.stream)
57-
logger.f(ioc, log_args)
57+
logger.formatter(ioc, log_args)
5858
write(logger.stream, take!(iob))
5959
logger.always_flush && flush(logger.stream)
6060
return nothing
6161
end
6262
shouldlog(logger::FormatLogger, arg...) = true
6363
min_enabled_level(logger::FormatLogger) = BelowMinLevel
6464
catch_exceptions(logger::FormatLogger) = true # Or false? SimpleLogger doesn't, ConsoleLogger does.
65+
66+
# For backwards compatibility
67+
function Base.getproperty(logger::FormatLogger, f::Symbol)
68+
return if f === :f
69+
getfield(logger, :formatter)
70+
else
71+
getfield(logger, f)
72+
end
73+
end

test/runtests.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ end
197197
end
198198
end
199199

200+
Base.@kwdef struct BasicLogFormatter
201+
include_module::Bool=true
202+
end
203+
204+
function (formatter::BasicLogFormatter)(io::IO, args::NamedTuple)
205+
if formatter.include_module
206+
print(io, args._module, " | ")
207+
end
208+
println(io, "[", args.level, "] ", args.message)
209+
end
210+
200211
@testset "FormatLogger" begin
201212
io = IOBuffer()
202213
logger = FormatLogger(io) do io, args
@@ -242,6 +253,21 @@ end
242253
l = read(f, String)
243254
@test startswith(l, "log message")
244255
end
256+
257+
# test function-like objects/functor are supported
258+
io = IOBuffer()
259+
with_logger(FormatLogger(BasicLogFormatter(; include_module=true), io)) do
260+
@info "test message"
261+
end
262+
str = String(take!(io))
263+
@test str == "$(@__MODULE__()) | [Info] test message\n"
264+
265+
io = IOBuffer()
266+
with_logger(FormatLogger(BasicLogFormatter(; include_module=false), io)) do
267+
@warn "test message"
268+
end
269+
str = String(take!(io))
270+
@test str == "[Warn] test message\n"
245271
end
246272

247273
@testset "LevelOverrideLogger" begin

0 commit comments

Comments
 (0)