1-
2- struct FormatLogger <: AbstractLogger
3- f:: Function
1+ struct FormatLogger{T} <: AbstractLogger
2+ formatter:: T
43 stream:: IO
54 always_flush:: Bool
65end
76
87"""
9- FormatLogger(f::Function , io::IO=stderr; always_flush=true)
8+ FormatLogger(formatter , io::IO=stderr; always_flush=true)
109
1110Logger 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)`
13- where `log_args ` has the following fields:
11+ The formatting function or callable object should be of the form
12+ `formatter(io::IOContext, log::NamedTuple)` where `log ` has the following fields:
1413`(level, message, _module, group, id, file, line, kwargs)`.
15- See [`LoggingExtras.handle_message_args`](@ref) for more information on what field is.
14+
15+ See [`LoggingExtras.handle_message_args`](@ref) for more information on what each field is.
1616
1717# Examples
1818```julia-repl
1919julia> using Logging, LoggingExtras
2020
21- julia> logger = FormatLogger() do io, args
22- println(io, args ._module, " | ", "[", args .level, "] ", args .message)
21+ julia> logger = FormatLogger() do io, log
22+ println(io, log ._module, " | ", "[", log .level, "] ", log .message)
2323 end;
2424
2525julia> with_logger(logger) do
@@ -30,31 +30,31 @@ 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
40- Logger sink that formats the message and writes it to the file at `path`. This is similar
41- to `FileLogger` except that it allows specifying the printing format.
40+ Logger sink that formats the message and writes it to the file at `path`. This is similar
41+ to [ `FileLogger`](@ref) 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... )
52- log_args = handle_message_args (args... ; kwargs... )
52+ log = handle_message_args (args... ; kwargs... )
5353 # We help the user by passing an IOBuffer to the formatting function
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 )
5858 write (logger. stream, take! (iob))
5959 logger. always_flush && flush (logger. stream)
6060 return nothing
0 commit comments