Skip to content

Commit

Permalink
Accept frequencies for logging rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Oct 29, 2017
1 parent 986a2d9 commit b544d8e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 40 deletions.
101 changes: 61 additions & 40 deletions loguru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ def make_should_rotate_function(self, rotation):
interval = self.parse_duration(rotation)
if interval is not None:
return self.make_should_rotate_function(interval)
frequency = self.parse_frequency(rotation)
if frequency is not None:
return self.make_should_rotate_function(frequency)
daytime = self.parse_daytime(rotation)
if daytime is not None:
day, time = daytime
Expand Down Expand Up @@ -308,46 +311,6 @@ def function(logs):

return function

@staticmethod
def parse_daytime(daytime):
daytime = daytime.strip()

daytime_reg = re.compile(r'(.*?)\s+at\s+(.*)', flags=re.I)
day_reg = re.compile(r'w\d+', flags=re.I)
time_reg = re.compile(r'[\d\.\:\,]+(?:\s*[ap]m)?', flags=re.I)

daytime_match = daytime_reg.fullmatch(daytime)
if daytime_match:
day, time = daytime_match.groups()
elif time_reg.fullmatch(daytime):
day, time = None, daytime
elif day_reg.fullmatch(daytime) or daytime.upper() in DAYS_NAMES:
day, time = daytime, None
else:
return None

if day is not None:
if day_reg.fullmatch(day):
day = int(day[1:])
if not 0 <= day <= 6:
raise ValueError("Invalid weekday index while parsing daytime: '%d'" % day)
elif day.upper() in DAYS_NAMES:
day = DAYS_NAMES.index(day.upper())
else:
raise ValueError("Invalid weekday value while parsing daytime: '%s'" % day)

if time is not None:
time_ = time
try:
time = pendulum.parse(time, strict=True)
except Exception as e:
raise ValueError("Invalid time while parsing daytime: '%s'" % time) from e
else:
if not isinstance(time, datetime.time):
raise ValueError("Cannot strictly parse time from: '%s'" % time_)

return day, time

@staticmethod
def parse_size(size):
size = size.strip()
Expand Down Expand Up @@ -404,6 +367,64 @@ def parse_duration(duration):

return pendulum.Interval(seconds=seconds)

@staticmethod
def parse_frequency(frequency):
frequency = frequency.strip().lower()
function = None

if frequency == 'hourly':
function = lambda t: t.add(hours=1).start_of('hour')
elif frequency == 'daily':
function = '00:00'
elif frequency == 'weekly':
function = 'w0'
elif frequency == 'monthly':
function = lambda t: t.add(months=1).start_of('month')
elif frequency == 'yearly':
function = lambda t: t.add(years=1).start_of('year')

return function

@staticmethod
def parse_daytime(daytime):
daytime = daytime.strip()

daytime_reg = re.compile(r'(.*?)\s+at\s+(.*)', flags=re.I)
day_reg = re.compile(r'w\d+', flags=re.I)
time_reg = re.compile(r'[\d\.\:\,]+(?:\s*[ap]m)?', flags=re.I)

daytime_match = daytime_reg.fullmatch(daytime)
if daytime_match:
day, time = daytime_match.groups()
elif time_reg.fullmatch(daytime):
day, time = None, daytime
elif day_reg.fullmatch(daytime) or daytime.upper() in DAYS_NAMES:
day, time = daytime, None
else:
return None

if day is not None:
if day_reg.fullmatch(day):
day = int(day[1:])
if not 0 <= day <= 6:
raise ValueError("Invalid weekday index while parsing daytime: '%d'" % day)
elif day.upper() in DAYS_NAMES:
day = DAYS_NAMES.index(day.upper())
else:
raise ValueError("Invalid weekday value while parsing daytime: '%s'" % day)

if time is not None:
time_ = time
try:
time = pendulum.parse(time, strict=True)
except Exception as e:
raise ValueError("Invalid time while parsing daytime: '%s'" % time) from e
else:
if not isinstance(time, datetime.time):
raise ValueError("Cannot strictly parse time from: '%s'" % time_)

return day, time

def rotating_write(self, message):
if self.should_rotate(message):
self.rotate()
Expand Down
3 changes: 3 additions & 0 deletions tests/test_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def test_size_rotation(tmpdir, logger, size):
(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]),
('hourly', [0.9, 0.2, 0.8, 3, 1]),
('WEEKLY', [11, 2, 24 * 6, 24, 24 * 7]),
(' mOnthLY', [0, 24 * 13, 29 * 24, 60 * 24, 24 * 35]),
])
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 b544d8e

Please sign in to comment.