Skip to content

Commit ebf9ca8

Browse files
author
Andrey Oskin
committed
File append
1 parent 9e58d51 commit ebf9ca8

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
matrix:
1212
version:
1313
- '1.0'
14-
- '1.5'
14+
- '1'
1515
- 'nightly'
1616
os:
1717
- ubuntu-latest

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MiniLoggers"
22
uuid = "93f3dd0f-005d-4452-894a-a31841fa4078"
33
authors = ["Andrey Oskin"]
4-
version = "0.2.4"
4+
version = "0.2.5"
55

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

src/minilogger.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ struct MiniLogger{IOT1 <: IO, IOT2 <: IO, DFT <: DateFormat} <: AbstractLogger
1212
lastflush::Base.RefValue{Int}
1313
end
1414

15-
getio(io) = io
16-
getio(io::AbstractString) = open(io)
15+
getio(io, append) = io
16+
getio(io::AbstractString, append) = open(io, append ? "a" : "w")
1717

1818
getflushthreshold(x::Integer) = x
1919
getflushthreshold(x::TimePeriod) = Dates.value(Millisecond(x))
@@ -29,6 +29,7 @@ Supported keyword arguments include:
2929
* `ioerr` (default `stderr`): IO stream which is used to output log messages above `errlevel` level. Can be either `IO` or `String`, in the latter case it is treated as a name of the output file.
3030
* `errlevel` (default `Error`): determines which output IO to use for log messages. If you want for all messages to go to `io`, set this parameter to `MiniLoggers.AboveMaxLevel`. If you want for all messages to go to `ioerr`, set this parameter to `MiniLoggers.BelowMinLevel`.
3131
* `minlevel` (default: `Info`): messages below this level are ignored. For example with default setting `@debug "foo"` is ignored.
32+
* `append` (default: `false`): defines whether to append to output stream or to truncate file initially. Used only if `io` or `ioerr` is a file path.
3233
* `squash_message` (default: `true`): if `squash_message` is set to `true`, then message is squashed to a single line, i.e. all `\\n` are changed to ` ` and `\\r` are removed.
3334
* `flush` (default: `true`): whether to `flush` IO stream for each log message. Flush behaviour also affected by `flush_threshold` argument.
3435
* `flush_threshold::Union{Integer, TimePeriod}` (default: 0): if this argument is nonzero and `flush` is `true`, then `io` is flushed only once per `flush_threshold` milliseconds. I.e. if time between two consecutive log messages is less then `flush_threshold`, then second message is not flushed and will have to wait for the next log event.
@@ -46,13 +47,13 @@ Supported keyword arguments include:
4647
4748
Each keyword accepts color information, which should be added after colon inside curly brackets. Colors can be either from `Base.text_colors` or special keyword `func`, in which case is used automated coloring. Additionaly, `bold` modifier is accepted by the `format` argument. For example: `{line:red}`, `{module:cyan:bold}`, `{group:func}` are all valid parts of the format command.
4849
49-
Colour information is applied recursively without override, so `{line {module:cyan} group:red}` is equivalent to `{line:red} {module:cyan} {group:red}`.
50+
Colour information is applied recursively without override, so `{{line} {module:cyan} {group}:red}` is equivalent to `{line:red} {module:cyan} {group:red}`.
5051
5152
If part of the format is not a recognised keyword, then it is just used as is, for example `{foo:red}` means that output log message contain word "foo" printed in red.
5253
"""
53-
function MiniLogger(; io = stdout, ioerr = stderr, errlevel = Error, minlevel = Info, message_limits = Dict{Any, Int}(), flush = true, format = "{[{datetime}]:func} {message}", dtformat = dateformat"yyyy-mm-dd HH:MM:SS", squash_message = true, flush_threshold = 0)
54-
tio = getio(io)
55-
tioerr = io == ioerr ? tio : getio(ioerr)
54+
function MiniLogger(; io = stdout, ioerr = stderr, errlevel = Error, minlevel = Info, append = false, message_limits = Dict{Any, Int}(), flush = true, format = "{[{datetime}]:func} {message}", dtformat = dateformat"yyyy-mm-dd HH:MM:SS", squash_message = true, flush_threshold = 0)
55+
tio = getio(io, append)
56+
tioerr = io == ioerr ? tio : getio(ioerr, append)
5657
lastflush = Dates.value(Dates.now())
5758
MiniLogger(tio, tioerr, errlevel, minlevel, message_limits, flush, tokenize(format), dtformat, squash_message, getflushthreshold(flush_threshold), Ref(lastflush))
5859
end

test/test03_misc.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,36 @@ Base.write(io::MockIO, a::Vector{UInt8}) = push!(io.recs, MockIORecord(String(co
5353
@test io.recs[3].buf == "Baz\n"
5454
end
5555

56+
@testset "file logs" begin
57+
fpath = joinpath(@__DIR__, "logtest1.log")
58+
logger = MiniLogger(io = fpath, format = "{message}")
59+
with_logger(logger) do
60+
@info "Foo"
61+
end
62+
open(fpath, "r") do f
63+
@test read(f, String) == "Foo\n"
64+
end
65+
66+
logger = MiniLogger(io = fpath, format = "{message}", append = true)
67+
with_logger(logger) do
68+
@info "Bar"
69+
end
70+
open(fpath, "r") do f
71+
@test read(f, String) == "Foo\nBar\n"
72+
end
73+
74+
logger = MiniLogger(io = fpath, format = "{message}")
75+
with_logger(logger) do
76+
@info "Baz"
77+
end
78+
open(fpath, "r") do f
79+
@test read(f, String) == "Baz\n"
80+
end
81+
try
82+
rm(fpath, force = true)
83+
catch err
84+
@error "" exception = (err, catch_backtrace())
85+
end
86+
end
87+
5688
end # module

0 commit comments

Comments
 (0)