Skip to content

Commit 3dfec54

Browse files
authored
Merge pull request #54 from fredrikekre/fe/datformat
DatetimeRotatingFileLogger: allow for passing DateFormats as input.
2 parents 8d8e8db + 2c04837 commit 3dfec54

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
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.6"
4+
version = "0.4.7"
55

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

src/datetime_rotation.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using Dates
2-
import Base: isless
32

43
raw"""
54
DatetimeRotatingFileLogger(dir, file_pattern; always_flush=true, rotation_callback=identity)
65
DatetimeRotatingFileLogger(f::Function, dir, file_pattern; always_flush=true, rotation_callback=identity)
76
87
Construct a `DatetimeRotatingFileLogger` that rotates its file based on the current date.
9-
The constructor takes a log output directory, `dir`, and a filename pattern.
10-
The filename pattern given is interpreted through the `Dates.format()` string formatter,
11-
allowing for yearly all the way down to minute-level log rotation. Note that if you
12-
wish to have a filename portion that is not interpreted as a format string, you may need
13-
to escape portions of the filename, as shown in the example below.
8+
The constructor takes a log output directory, `dir`, and a filename pattern. The smallest
9+
time resolution in the format string determines the frequency of log file rotation,
10+
allowing for yearly all the way down to minute-level log rotation.
11+
12+
The pattern can be given as a string or as a `Dates.DateFormat`. Note that if you
13+
wish to have a filename portion that should not be interpreted as a format string, you may
14+
need to escape portions of the filename, as shown in the example below.
1415
1516
It is possible to pass a formatter function as the first argument to control the output.
1617
The formatting function should be of the form `f(io::IOContext, log_args::NamedTuple)`
@@ -61,9 +62,10 @@ function DatetimeRotatingFileLogger(f::Union{Function,Nothing}, dir, filename_pa
6162
else # f isa Function
6263
FormatLogger(f, IOBuffer(); always_flush=false) # no need to flush twice
6364
end
65+
filename_pattern isa DateFormat || (filename_pattern = DateFormat(filename_pattern))
6466
# abspath in case user constructs the logger with a relative path and later cd's.
6567
drfl = DatetimeRotatingFileLogger(logger, abspath(dir),
66-
DateFormat(filename_pattern), now(), always_flush, ReentrantLock(), nothing, rotation_callback)
68+
filename_pattern, now(), always_flush, ReentrantLock(), nothing, rotation_callback)
6769
reopen!(drfl)
6870
return drfl
6971
end

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ end
183183
# Sub-minute resolution not allowed
184184
@test_throws(ArgumentError("rotating the logger with sub-minute resolution not supported"),
185185
DatetimeRotatingFileLogger(dir, "HH-MM-SS"))
186+
187+
# Test constructors with pattern as a DateFormat
188+
l = DatetimeRotatingFileLogger(dir, raw"yyyy-mm-dd.\l\o\g")
189+
l1 = DatetimeRotatingFileLogger(dir, dateformat"yyyy-mm-dd.\l\o\g")
190+
l2 = DatetimeRotatingFileLogger(identity, dir, dateformat"yyyy-mm-dd.\l\o\g")
191+
@test l.filename_pattern == l1.filename_pattern == l2.filename_pattern
186192
end
187193
end
188194

0 commit comments

Comments
 (0)