Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LoggingExtras"
uuid = "e6f89c97-d47a-5376-807f-9c37f3926c36"
authors = ["Lyndon White <[email protected]>"]
version = "0.4.6"
version = "0.4.7"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
16 changes: 9 additions & 7 deletions src/datetime_rotation.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using Dates
import Base: isless

raw"""
DatetimeRotatingFileLogger(dir, file_pattern; always_flush=true, rotation_callback=identity)
DatetimeRotatingFileLogger(f::Function, dir, file_pattern; always_flush=true, rotation_callback=identity)

Construct a `DatetimeRotatingFileLogger` that rotates its file based on the current date.
The constructor takes a log output directory, `dir`, and a filename pattern.
The filename pattern given is interpreted through the `Dates.format()` string formatter,
allowing for yearly all the way down to minute-level log rotation. Note that if you
wish to have a filename portion that is not interpreted as a format string, you may need
to escape portions of the filename, as shown in the example below.
The constructor takes a log output directory, `dir`, and a filename pattern. The smallest
time resolution in the format string determines the frequency of log file rotation,
allowing for yearly all the way down to minute-level log rotation.

The pattern can be given as a string or as a `Dates.DateFormat`. Note that if you
wish to have a filename portion that should not be interpreted as a format string, you may
need to escape portions of the filename, as shown in the example below.

It is possible to pass a formatter function as the first argument to control the output.
The formatting function should be of the form `f(io::IOContext, log_args::NamedTuple)`
Expand Down Expand Up @@ -61,9 +62,10 @@ function DatetimeRotatingFileLogger(f::Union{Function,Nothing}, dir, filename_pa
else # f isa Function
FormatLogger(f, IOBuffer(); always_flush=false) # no need to flush twice
end
filename_pattern isa DateFormat || (filename_pattern = DateFormat(filename_pattern))
# abspath in case user constructs the logger with a relative path and later cd's.
drfl = DatetimeRotatingFileLogger(logger, abspath(dir),
DateFormat(filename_pattern), now(), always_flush, ReentrantLock(), nothing, rotation_callback)
filename_pattern, now(), always_flush, ReentrantLock(), nothing, rotation_callback)
reopen!(drfl)
return drfl
end
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ end
# Sub-minute resolution not allowed
@test_throws(ArgumentError("rotating the logger with sub-minute resolution not supported"),
DatetimeRotatingFileLogger(dir, "HH-MM-SS"))

# Test constructors with pattern as a DateFormat
l = DatetimeRotatingFileLogger(dir, raw"yyyy-mm-dd.\l\o\g")
l1 = DatetimeRotatingFileLogger(dir, dateformat"yyyy-mm-dd.\l\o\g")
l2 = DatetimeRotatingFileLogger(identity, dir, dateformat"yyyy-mm-dd.\l\o\g")
@test l.filename_pattern == l1.filename_pattern == l2.filename_pattern
end
end

Expand Down