Skip to content

Commit

Permalink
Fix usage of aware "datetime.time" for file rotation (Delgan#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Jan 6, 2023
1 parent a08cd51 commit 20eb57f
Show file tree
Hide file tree
Showing 3 changed files with 451 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add a new ``watch`` optional argument to file sinks in order to automatically re-create possibly deleted or changed file (`#471 <https://github.com/Delgan/loguru/issues/471>`_).
- Make ``patch()`` calls cumulative instead of overriding the possibly existing patching function (`#462 <https://github.com/Delgan/loguru/issues/462>`_).
- Avoid possible deadlocks caused by re-using the logger inside a sink, a signal handler or a ``__del__`` method. Since the logger is not re-entrant, such misuse will be detected and will now generate a ``RuntimeError`` (`#712 <https://github.com/Delgan/loguru/issues/712>`_, thanks `@jacksmith15 <https://github.com/jacksmith15>`_).
- Fix file sink rotation using an aware ``datetime.time`` for which the timezone was ignored (`#697 <https://github.com/Delgan/loguru/issues/697>`_).
- Fix logs colorization not automatically enabled for Jupyter Notebook and Google Colab (`#494 <https://github.com/Delgan/loguru/issues/494>`_).
- Fix logs colorization not automatically enabled for Github Actions and others CI platforms (`#604 <https://github.com/Delgan/loguru/issues/604>`_).
- Fix ``logger.complete()`` possibly hanging forever when ``enqueue=True`` and ``catch=False`` if internal thread killed due to ``Exception`` raised by sink (`#647 <https://github.com/Delgan/loguru/issues/647>`_).
Expand Down
41 changes: 31 additions & 10 deletions loguru/_file_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,44 @@ def __init__(self, step_forward, time_init=None):
self._limit = None

def __call__(self, message, file):
record_time = message.record["time"]

if self._limit is None:
filepath = os.path.realpath(file.name)
creation_time = get_ctime(filepath)
set_ctime(filepath, creation_time)
start_time = limit = datetime.datetime.fromtimestamp(creation_time)
if self._time_init is not None:
limit = limit.replace(
hour=self._time_init.hour,
minute=self._time_init.minute,
second=self._time_init.second,
microsecond=self._time_init.microsecond,
)
if limit <= start_time:
start_time = datetime.datetime.fromtimestamp(
creation_time, tz=datetime.timezone.utc
)

time_init = self._time_init

if time_init is None:
limit = start_time.astimezone(record_time.tzinfo).replace(tzinfo=None)
limit = self._step_forward(limit)
else:
limit = datetime.datetime(
start_time.year,
start_time.month,
start_time.day,
time_init.hour,
time_init.minute,
time_init.second,
time_init.microsecond,
record_time.tzinfo if time_init.tzinfo is None else time_init.tzinfo,
)

if limit <= start_time:
limit = self._step_forward(limit)

if time_init.tzinfo is None:
limit = limit.replace(tzinfo=None)

self._limit = limit

record_time = message.record["time"].replace(tzinfo=None)
if self._limit.tzinfo is None:
record_time = record_time.replace(tzinfo=None)

if record_time >= self._limit:
while self._limit <= record_time:
self._limit = self._step_forward(self._limit)
Expand Down
Loading

0 comments on commit 20eb57f

Please sign in to comment.