Skip to content

Commit 396afbb

Browse files
authored
added method for FormatLogger to write directly to file and some doc (#61)
1 parent 78df79e commit 396afbb

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LoggingExtras"
22
uuid = "e6f89c97-d47a-5376-807f-9c37f3926c36"
33
authors = ["Lyndon White <oxinabox@ucc.asn.au>"]
4-
version = "0.4.7"
4+
version = "0.4.8"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ It is really simple.
281281
The resulting file format is similar to that which is shown in the REPL.
282282
(Not identical, but similar)
283283
284+
**NOTE**: To print to the file in a specific format, e.g. to create a JSON log, use
285+
`FormatLogger` instead.
286+
284287
### Demo: `TeeLogger` and `FileLogger`
285288
We are going to log info and above to one file,
286289
and warnings and above to another.
@@ -349,13 +352,20 @@ in the constructor. See `FormatLogger` for the requirements on the formatter fun
349352
The `FormatLogger` is a sink that formats the message and prints to a wrapped IO.
350353
Formatting is done by providing a function `f(io::IO, log_args::NamedTuple)`.
351354
355+
`FormatLogger` can take as its second argument either a writeable `IO` or a filepath. The `append::Bool` keyword
356+
argument determines whether the file is opened in append mode (`"a"`) or truncate mode (`"w"`).
357+
352358
```julia
353359
julia> using LoggingExtras
354360

355361
julia> logger = FormatLogger() do io, args
356362
println(io, args._module, " | ", "[", args.level, "] ", args.message)
357363
end;
358364

365+
julia> logger = FormatLogger("out.log"; append=true) do io, args
366+
println(io, args._module, " | ", "[", args.level, "] ", args.message)
367+
end;
368+
359369
julia> with_logger(logger) do
360370
@info "This is an informational message."
361371
@warn "This is a warning, should take a look."

src/filelogger.jl

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ end
99
Create a logger sink that write messages to a file specified with `path`.
1010
To append to the file (rather than truncating the file first), use `append=true`.
1111
If `always_flush=true` the stream is flushed after every handled log message.
12+
13+
!!! note
14+
`FileLogger` uses the same output formatting as `SimpleLogger`. Use a `FormatLogger`
15+
instead of a `FileLogger` to control the output formatting.
16+
1217
"""
1318
function FileLogger(path; append=false, kwargs...)
1419
filehandle = open(path, append ? "a" : "w")
@@ -22,6 +27,11 @@ Create a logger sink that write messages to the `io::IOStream`. The stream
2227
is expected to be open and writeable.
2328
If `always_flush=true` the stream is flushed after every handled log message.
2429
30+
!!! note
31+
`FileLogger` uses the same output formatting as `SimpleLogger`. Use a `FormatLogger`
32+
instead of a `FileLogger` to control the output formatting.
33+
34+
2535
# Examples
2636
```julia
2737
io = open("path/to/file.log", "a") # append to the file

src/formatlogger.jl

+14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ function FormatLogger(f::Function, io::IO=stderr; always_flush=true)
3434
return FormatLogger(f, io, always_flush)
3535
end
3636

37+
"""
38+
FormatLogger(f::Function, path::AbstractString; append=false, always_flush=true)
39+
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.
42+
43+
To append to the file (rather than truncating the file first), use `append=true`.
44+
If `always_flush=true` the stream is flushed after every handled log message.
45+
"""
46+
function FormatLogger(f::Function, path::AbstractString; append::Bool=false, kw...)
47+
io = open(path, append ? "a" : "w")
48+
return FormatLogger(f, io; kw...)
49+
end
50+
3751
function handle_message(logger::FormatLogger, args...; kwargs...)
3852
log_args = handle_message_args(args...; kwargs...)
3953
# We help the user by passing an IOBuffer to the formatting function

test/runtests.jl

+16
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,22 @@ end
221221
logger = FormatLogger(x -> x; always_flush=false)
222222
@test logger.stream === stderr
223223
@test !logger.always_flush
224+
225+
# test file arguments
226+
mktempdir() do dir
227+
f = joinpath(dir, "test.log")
228+
229+
logger = FormatLogger(f) do io, args
230+
println(io, "log message")
231+
end
232+
233+
with_logger(logger) do
234+
@info "test message"
235+
end
236+
237+
l = read(f, String)
238+
@test startswith(l, "log message")
239+
end
224240
end
225241

226242
@testset "Deprecations" begin

0 commit comments

Comments
 (0)