11
22struct FormatLogger <: AbstractLogger
3- f :: Function
3+ formatter
44 stream:: IO
55 always_flush:: Bool
66end
77
88"""
9- FormatLogger(f::Function , io::IO=stderr; always_flush=true)
9+ FormatLogger(formatter , io::IO=stderr; always_flush=true)
1010
1111Logger 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)`
1313where `log_args` has the following fields:
1414`(level, message, _module, group, id, file, line, kwargs)`.
1515See [`LoggingExtras.handle_message_args`](@ref) for more information on what field is.
@@ -30,22 +30,22 @@ Main | [Info] This is an informational message.
3030Main | [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)
3535end
3636
3737"""
38- FormatLogger(f::Function , path::AbstractString; append=false, always_flush=true)
38+ FormatLogger(formatter , path::AbstractString; append=false, always_flush=true)
3939
4040Logger sink that formats the message and writes it to the file at `path`. This is similar
4141to `FileLogger` except that it allows specifying the printing format.
4242
4343To append to the file (rather than truncating the file first), use `append=true`.
4444If `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... )
4949end
5050
5151function 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
6161end
6262shouldlog (logger:: FormatLogger , arg... ) = true
6363min_enabled_level (logger:: FormatLogger ) = BelowMinLevel
6464catch_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
0 commit comments