Skip to content

Commit

Permalink
Merge pull request #54 from fredrikekre/fe/datformat
Browse files Browse the repository at this point in the history
DatetimeRotatingFileLogger: allow for passing DateFormats as input.
  • Loading branch information
oxinabox authored Jun 14, 2021
2 parents 8d8e8db + 2c04837 commit 3dfec54
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
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

2 comments on commit 3dfec54

@oxinabox
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/38835

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.7 -m "<description of version>" 3dfec54902f154f564fe16d77abd155193477d1e
git push origin v0.4.7

Please sign in to comment.