-
Notifications
You must be signed in to change notification settings - Fork 719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Log rotate by time and size #241
Comments
Hi. :) Loguru does not provide such possibility directly. To do this, you need to implement a custom def rotation(message, file):
file.seek(0, 2)
if file.tell() + len(message) > 10000:
return True
if message.record["time"].time() > datetime.time(12, 0, 0):
return True
return False
logger.add("file.log", rotation=rotation) |
Hi, you are right, this is one way to do this :) But i guess if message.record["time"].time() > datetime.time(12, 0, 0):
return True would rotate for every message after 12am? How would you write that for a timedelta? (Since it needs to know the start time of the current file?) |
@mahadi Oups, yeah, sorry. >.< Well, you probably need to use a from datetime import datetime, timedelta, time
class Rotator:
def __init__(self, size, time):
self._size = size
self._time = datetime.now().replace(hour=time.hour, minute=time.minute, second=time.second)
def should_rotate(self, message, file):
file.seek(0, 2)
if file.tell() + len(message) > self._size:
return True
if message.record["time"].timestamp() > self._time.timestamp():
self._time += timedelta(days=1)
return True
return False
rotator = Rotator(10000, time(12, 0, 0))
logger.add("file.log", rotation=rotator.should_rotate) |
Thanks for the hint! Will use it :) Btw: |
Great, glad it worked for you! 😄 I'm closing this issue then. 👍 |
Just for completeness: This is a small fix to prevent immediate log rotation if the current time is already past the given time: class Rotator:
def __init__(self, size, at):
self._size = size
now = datetime.now()
today_at_time = now.replace(hour=at.hour, minute=at.minute, second=at.second)
if now >= today_at_time:
# the current time is already past the target time so it would rotate already
# add one day to prevent an immediate rotation
self._next_rotate = today_at_time + timedelta(days=1)
else:
self._next_rotate = today_at_time
def should_rotate(self, message, file):
file.seek(0, 2)
if file.tell() + len(message) > self._size:
return True
if message.record["time"].timestamp() > self._next_rotate.timestamp():
self._next_rotate += timedelta(days=1)
return True
return False |
that't great!, here a small fix:
and the test code
|
And if i would like to rotate every hour and every time log exceeds a file size ? |
@mancioshell Surely you can adapt the |
@Delgan I'm sorry, I explained myself badly :) |
Then your question is actually, how to achieve a rotation every hour, right? Because the size issue is already solved above. |
@mahadi yep. I would like to rotate the log every hour or every time it exceeds 100Mb of size. Any advice to edit the example in the docs for my use case? |
@mancioshell Did you try amending the snippet with |
@Delgan could the following code works ?
|
@mancioshell I guess it should. :) |
if I want loguru rotate everytime I run the thread,How should I do ? |
@kanghaov Please can you open a new ticket describing more precisely the issue you're facing? |
Hi, is it possible to rotate a logfile by duration and file size (and finally compress it)? Currently i only see that o can choose duration or file size but not both at the same time.
The text was updated successfully, but these errors were encountered: