Skip to content

Commit

Permalink
Accept function for FileSink rotation argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Oct 28, 2017
1 parent d5e9534 commit a4b8008
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions loguru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ def function(message):
self.rotation_time = time_limit
return True
return False
elif callable(rotation):
time_limit = rotation(self.start_time)
def function(message):
nonlocal time_limit
record_time = message.record['time']
if record_time >= time_limit:
time_limit = rotation(record_time)
return True
return False
else:
raise ValueError("Cannot infer rotation for objects of type: '%s'" % type(rotation))

Expand Down
1 change: 1 addition & 0 deletions tests/test_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def test_size_rotation(tmpdir, logger, size):
("1.222 hours, 3.44s", [1.222, 0.1, 1, 1.2, 2]),
(datetime.timedelta(hours=1), [0.9, 0.2, 0.7, 0.5, 3]),
(pendulum.Interval(minutes=30), [0.48, 0.04, 0.07, 0.44, 0.5]),
(lambda t: t.add(months=1).start_of('month').at(13, 00, 00), [12 * 24, 26, 31 * 24 - 2, 2, 24 * 60]),
])
def test_time_rotation(monkeypatch, tmpdir, when, hours, logger):
now = pendulum.parse("2017-06-18 12:00:00") # Sunday
Expand Down

0 comments on commit a4b8008

Please sign in to comment.