-
Notifications
You must be signed in to change notification settings - Fork 705
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 rotation ... controlling the name of the rotated file #213
Comments
Hi. You can use a custom formatter by specifying the logger.add("file-{time:YYYYMMDD-HHMM!UTC}.log", rotation="5 MB") Only downside is that if you stop and restart your application, it will create a new file instead of continuing to write to the previous one until rotation threshold is reached. If that's a problem, I think you can use the |
Hi @Delgan ,
and my program encountered the following exception when entering March
It seems that Feb's log was properly rotated out and compressed while the program still wanted to write to it. Is there a way to fix this? Thank you! |
@jakane Are you ok with the snippet I shared to modify the name of the rotated file? |
@Delgan, And whenever it rotates: -> "app.out2021-05-12.2021-05-12_01-00-00_002130.log" loguru version: 0.5.3 Am I doing wrong, or is there any good code snippet to handle this problem? I really desperately need this... |
@bybatkhuu Yes, if you use the You're seeing file named
This behavior makes sense if you use In your case, if your application was started the 2021-05-11 and ran continuously without being restarted, then you would have had two files named I think you can achieve what you're looking for by using a custom import os
from datetime import datetime
def rename_rotated_file(filepath):
now = datetime.now()
new_path = os.path.dirname(filepath) + "/app.out{:%Y-%m-%d}.log".format(now)
os.rename(filepath, new_path)
logger.add("app.out.log", rotation="01:00", compression=rename_rotated_file) |
@bybatkhuu Feel free to open a new issue if you want to investigate this further. 😉 Maybe there is a way to avoid modifying Loguru's source code, and thus to benefit easily from the next updates. |
@Delgan I wonder if it is possible to (optionally) override |
@mandarvaze I'm not really planning to expose this function to the public API. What is bothering you with the |
@Delgan When I debugged, I reached |
Hi @Delgan , i would like to customize rotate file name with custom |
@mancioshell I'm not sure if I fully understand your problem, but I don't think it's possible. Can you check that |
@Delgan I'll try to explain my problem with more details. My goal is to rotate the file in 2 cases (or if it exceeds a particular size e.g. 100 MB, or every hour). My rotated filename should be : /var/log/platform/{yyyy-MM-dd_HH}.app.sdk-log.zip If an application exceeds the 100 MB size in the same hour, it could override the existing zip file. So i need an index to track the already existing rotated files, so i could write to a file with the following pattern: where %i is the index Is it possible ? |
@mancioshell Thanks for the example. There is no such index available, you need to enumerate the existing files until you find an available index, or increment an index from a variable. |
I would like log rotation to work similar to a common pattern used in logrotate(8).
ie: current logs going to file.log
rotated files going to file-YYYYmmdd-HHMM.log
What I wish to do is control the {time} portion of the rotated file name. I would like to have something like '-{time:YYYYMMDD-HHMM!UTC}' instead of what looks like '.YYYY-MM-DD_HH-MM-SS_SSSSSS}' (where it is in local time).
How would I go about this? Otherwise the rotation, compression, retention works as I would expect.
Thanks
The text was updated successfully, but these errors were encountered: