-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add support for automatic removal of old logs #432
Conversation
GetOverdueLogNames(string log_directory, int days) will check all filenames under log_directory, and return a list of files whose last modified time is over the given days (calculated using difftime()). So that we can easily for unlink all files stored in the returned vector.
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
Googlers can find more info about SignCLA and this PR by following this link. |
I signed it! |
CLAs look good, thanks! Googlers can find more info about SignCLA and this PR by following this link. |
Guess the patch was implemented based on Linux. For Windows user, https://github.com/tronkko/dirent will be needed. |
e42db36
to
55cfc38
Compare
In this commit, at the end of LogFileObject::Write, it will perform clean up for old logs. It uses GetLoggingDirectories() and for each file in each directory, it will check if a file is a log file produced by glog. If it is, and it is last modified 3 days ago, then it will unlink() this file. (It will only remove the project's own log files, it won't remove the logs from other projects.) Currently it is hardcoded to 3 days, I'll see if this can be implemented in a more flexible manner.
The log cleaner can be enabled and disabled at any given time. By default, the log cleaner is disabled. For example, this will enable the log cleaner and delete the log files whose last modified time is >= x days google::EnableLogCleaner(x days); To disable it, simply call google::DisableLogCleaner(); Please note that it will only clean up the logs produced for its own project, the log files from other project will be untouched.
Also replaced the hardcoded overdue days with the correct variable.
Thank you very much for your help! CI has passed on windows! To enable log cleaner, in your project (log cleaner is disabled by default).
And then your project will check if there are overdue logs whenever a flush is performed. In this example, the logs whose last modified times are >= 3 will be unlink(). It calls GetLoggingDirectories() to look for overdue logs. You may also disable it at anytime.
I try to check if a file is a glog file produced by the project it is linked in carefully. It will only remove a log if its filename matches: Line 871 in b8a61d7
Edit: |
Previously the full path to a file is passed into IsGlogLog(), and then std::string::erase() is used to get the filename part. If a directory name contains '.', then this function will be unreliable. Now only the filename it self is passed into IsGlogLog(), so this problem will be eradicated.
Splitting a filename into tokens by '.' causes problems if the executable's filename contains a dot. Filename should be matched keyword by keyword in the following order: 1. program name 2. hostname 3. username 4. "log"
@sergiud Sorry for asking but any chance this patch could be merged? I've tested them on linux (4.19.66-gentoo) and windows 10, old log files can be removed automatically. This feature is disabled by default. |
@aesophor If I use SetLogDestination API to change base_filename_, old logs will not delete automatically. is a bug? |
This PR is in regard to #423. The code has been added and tested , but not yet integrated into glog.
Example usage:
GetOverdueLogNames() will check all filenames under log_directory, and return a list of files whose last modified time is over the given days ( calculated with
difftime()
), so that we can simply call unlink() on the filenames in the returned vector.However, I'm not sure when is the right time to call it.