-
Notifications
You must be signed in to change notification settings - Fork 730
Log file rotation not rotating file #58
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
Comments
Think I might have solved this one myself. I guess I wanted retention rather than rotation? As per the docstrings in _logger.py;
My file is opened and closed each time the file is run, I believe the above refers to a file being open for an extended period of time (in my case, a week) before the rotation will close and start a new one? In which case it won't apply to my situation? |
Hi @turner-prize. As you understood, The There is currently no way to rotate your log file based on the time it was created, supposing you open and close the same file each time you run your application. |
Ah that makes sense. I should have read the docs a bit closer! I might just add a function before the logger config to check how old the log is, and rename it with an appended date if over a week old, which will do the job. Love the library by the way! |
You could also pass your function to the def should_rotate(message, file):
filepath = os.path.abspath(file.name)
creation = os.path.getmtime(filepath)
now = message.record["time"].timestamp()
maxtime = 7 * 24 * 60 * 60 # 1 week in seconds
return now - creation > maxtime
logger.add("file.log", rotation=should_rotate)
Thanks! |
I'm reopening this issue as I think I may implement the workaround directly into Loguru. It makes more sense to have the |
I am testing loguru currently and was confused that the rotation behaves like this. I appreciate that you will implement the rotation based on creation date |
I think so, too. Thank you |
|
Ok, I finally find the time to update the As @YJinHai correctly noticed, the first snipped I provided was wrong because based on the Actually, there is no standard cross-platform way to retrieve the creation time of a file. Python doesn't provide such utility and it's not easily accessible on Linux systems. See this question on SO. Consequently, I had to implement a different policy on each platform:
This should work in most cases. |
I have a log configured as per the below:
logger.add("path/to/log.log",
format="{time:DD/MM/YY HH:mm:ss} - {message}",
level="CustomLogLevel",
filter=lamba record: record['level']="CustomLogLevel",
rotation="1 week")
The script which the above logger is linked to runs once per hour, for about 30 seconds. All works fine except the rotation, it's been configured since the 6th of Feb and hasn't created a new file yet, just keeps appending to the file which was initially created. Is this configured wrong on my end?
Edit:
Just read the contributions page!
using:
Python 3.7.2
Loguru 0.2.5
Windows Server 2016
Using both VS Code and terminal.
The text was updated successfully, but these errors were encountered: