-
Notifications
You must be signed in to change notification settings - Fork 704
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
Compression & rotation? #229
Comments
Hi @algaspar. I just tested (with a different rotation hour) and it worked fine on my computer. After the rotation time, I could see Which version of Loguru are you using? You can display it with |
Thanks for your quick reply :-) I just checked. I am running 0.4.1 on Windows Server 2016. I'm wondering if it has anything to do with my naming. Early on in my testing I hadn't started using anything but 'sc.log' for the file name. I had two of those still in my test folder that had been zipped and renamed sc.2020-03-06_21-00-05_340212.log.zip and sc.2020-03-13_23-00-02_171550.log.zip. Another one, though, was just named sc.log.zip; so I'm not sure how it decides to add a time stamp... This morning I have a new SC--Thu--Mar-26.log, but the two earlier ones for Tuesday and Wednesday remain unzipped. I'll throw together a test script that just logs to a sink that rotates quickly (every minute) and see if that zips... Thanks-- Al |
OK. I was able to get it to compress, and I was also able to duplicate the issue that I was having. If my log file retains the same name, the zip works. If my log file was named sc--{time:ddd--HH}.log with rotation set to 1 minute, the file got zipped. However, if I named the file sc--{time:ddd--mm}.log, loguru created a new log on the next minute, but it did not zip the previous log. (On a side note while HH and mm worked by themselves, neither HH:mm or mm:HH worked; I got an empty file with the name truncated at whichever (mm or HH) appeared before the ':'. The .log extension was not there.). I've uploaded a copy of the little test file that I used. Any ideas how to get around this? Thanks for your help-- Al |
Thanks for the test file! I understand the difficulties you're going through. You see, when you set the file name to be So, supposing you start your application à 11:44:20 and stop it immediately. Created file is Is your application supposed to run continuously, without being halted and restarted? In such a case, configuring with |
OK. That makes sense. I added a third sink to my test file, which is identical except for the name. I named it just 'sc.log' without any time parameters. I ran the my test script over a three minute period and got the following three files: sc3.2020-03-27_10-10-40_978726.log.zip sc.log is the newest, and the one with the timestamp is the oldest. Subsequent timestamped files use the same timestamp and add a number (i.e., My "application" consists of several scripts that run daily at different times on a prearranged schedule. They all write to the same log file. I wanted to be able to keep the logs restricted to one days worth of data, and I thought it would be convenient to have the day and date as part of the file name. I was also hoping to zip the logs that were no longer active. As it essentially runs continuously setting I'm thinking that I could do something like that with a flat filename (like sc.log) and have compression set to a callable, which renamed the rotating file to something like sc-Thu--Mar-23.log and zipped it? That way I could easily see which file was active and the days and dates for the others. Would rotation also need to be a callable? Would retention recognize that it needed to remove these custom compressed files after the retention period, or would I need to build that into my compression callable, have retention be a callable as well, or build a 'sweep' app? Thanks-- Al |
It seemed like a callable for correction would be the answer. I imported os, time, datetime, and zipfile, and created this callable:
Since I want the file to change nightly at midnight, I figured that I just needed to check if the day of the week in the two structs was different. However, I didn't want to wait 24 hours between tests; so I used minute (item 4 in the time struct) rather than weekday (item 6 commented out above), and had my check be
It works, but the first time it runs (and on rotation) I get a new.log file with my log entries and an empty file with a time stamp like this:
Subsequent runs of my test file correctly write to new.log and zip the file on rotation, but when it does rotate, I get another empty Should I be doing this with a rotation callable instead or a combination? Will I need a callable for retention to remove my custom zip files that are older than my retention date? Bottom line, what am I getting wrong here ;-)? Thanks-- Al |
Hey @algaspar, that's an interesting use case, I think you indeed need to use a custom However, you should not have to handle rotation times in your from loguru import logger
from datetime import datetime
from zipfile import ZipFile
import os
def mycompress(filepath):
archive_path = "sc.{:%Y-%m-%d}.log.zip".format(datetime.now())
with ZipFile(archive_path, "w") as archive:
archive.write(filepath, arcname="sc.log")
os.remove(filepath)
logger.add("sc.log", rotation="00:00", compression=mycompress) A few more notes about your previous comments:
|
Thank you so much for your patience and your detailed explanation. I modified my compress callable as you suggested. It works great; the odd zero length file is gone :-). Since the log file I am archiving contains data from the day before, I used yesterday's date. Here's what I came up with:
I look forward to your retention change, but in the meantime, I'll go work on a retention callable using os.listdir() as you suggest. Thank you very much for an awesome library. Loguru really makes logging simple and even pleasurable. By the way, is it "log guru" or does it rhyme with "kangaroo"? :-) --Al |
Great, glad this solved your problem! And thanks for the kind words, I appreciate it! It's supposed to be pronounced as "guru", but honestly, I'm not even sure I pronounce it correctly myself. 😅 |
😃 Quick retention question. The documentation says that the retention callable "should accept the list of log files as argument", but above you say that I need to use os.listdir() to find my files. What does loguru pass to a retention callable? Thanks-- Al |
Loguru will pass a list of file paths to the |
Ah, gotcha. That makes sense. Will do. Thanks! --Al |
I have improved the logs gathering. The files collected for the The fix will be available in the next release. 👍 |
I just wanted to say thank you 😄 |
I am having a little trouble with getting my log files to compress when they're done with, and I'm presuming it has something to do with the settings that I have.
I want my logs to start with a new file at midnight every day and I want them to be compressed when they are closed. I get my settings from a configuration file, which currently has these settings:
log_file = ../logs/SC--{time:ddd--MMM-DD}.log
log_format = {time:MM-DD-YY at HH:mm} | {level} | {file}:{line} | {function} | {message}
log_rotation = 00:00
log_retention = 1 month
log_compression = zip
log_level = INFO
log_queue = True
All of these settings except compression appear to work. I read these settings in from the configuration file and assign them like this:
logger.add(log_file, format=log_format, rotation=log_rotation,
retention=log_retention, compression=log_compression,
enqueue=log_queue, level=log_level)
The documentation says, "The compression happens at rotation or at sink stop if rotation is None." I presumed then that when my new log file is created at midnight, my previous day's log file would be zipped. That isn't happening. The new file is created, but the old one stays there unzipped. For example, yesterday had a file named SC--Tue--Mar-24.log. Today's log was created as SC--Wed--Mar-25.log, but when it was created SC--Tue--Mar-24.log remained as is, unzipped
Can someone tell me what I might be doing wrong?
Thanks--
Al
The text was updated successfully, but these errors were encountered: