Skip to content
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

Support rolling log files base on file size and time #811

Closed
wants to merge 1 commit into from

Conversation

yangzhg
Copy link

@yangzhg yangzhg commented Mar 22, 2022

This pr implements #809
Add two flags max_logfile_num and log_rolling_policy, support rolling
log file by day or by hour, and use max_logfile_num to control the max
history log file num after rolling.

@sergiud sergiud added this to the next milestone Apr 3, 2022
Copy link
Collaborator

@sergiud sergiud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I have several remarks. Could you also please rebase?

src/logging.cc Outdated Show resolved Hide resolved
src/logging.cc Outdated Show resolved Hide resolved
src/logging.cc Outdated
@@ -476,6 +494,9 @@ class LogFileObject : public base::Logger {
unsigned int rollover_attempt_;
int64 next_flush_time_; // cycle count at which to flush log
WallTime start_time_;
std::list<Filetime> file_list_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for choosing a std::list here? Looks like you want a std::priority_queue here since you are constantly sorting the container elements.

Also the member variable name should indicate what kind of times are stored.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review, it is because of need to access the last element @

const char *filename = file_list_.back().name.c_str();

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not a good reason for choosing std::list. Given you are continually sorting, std::priority_queue is probably a better choice.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe std::set is a better choice? It is ordered and have access to first and last element.

Copy link
Collaborator

@sergiud sergiud Aug 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, std::set is only useful if you lookup specific (unique) elements which you don't. The container also doesn't really give direct access to the top element. This is what std::priority_queue is for.

src/logging.cc Outdated Show resolved Hide resolved
src/logging.cc Outdated Show resolved Hide resolved
src/logging.cc Show resolved Hide resolved
src/logging.cc Outdated Show resolved Hide resolved
src/logging.cc Outdated
@@ -1148,13 +1221,50 @@ void LogFileObject::Write(bool force_flush,
if (base_filename_selected_ && base_filename_.empty()) {
return;
}

if (file_length_ >> 20U >= MaxLogSize() || PidHasChanged()) {
struct ::tm tm_time;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following block does quite some work. Consider moving it into a separate method and add some comments describing the overall purpose and the non-obvious logic.

src/logging.cc Outdated Show resolved Hide resolved
src/logging.cc Outdated
@@ -1164,7 +1274,15 @@ void LogFileObject::Write(bool force_flush,
if (++rollover_attempt_ != kRolloverAttemptFrequency) return;
rollover_attempt_ = 0;

struct ::tm tm_time;
if (!inited_) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments of what happens here would be nice.

@yangzhg
Copy link
Author

yangzhg commented Aug 4, 2022

@sergiud I have edited according to your comments, please review again

@codecov-commenter
Copy link

codecov-commenter commented Aug 4, 2022

Codecov Report

Merging #811 (5297433) into master (a34226c) will decrease coverage by 0.31%.
The diff coverage is 69.09%.

@@            Coverage Diff             @@
##           master     #811      +/-   ##
==========================================
- Coverage   73.19%   72.87%   -0.32%     
==========================================
  Files          17       17              
  Lines        3279     3366      +87     
==========================================
+ Hits         2400     2453      +53     
- Misses        879      913      +34     
Impacted Files Coverage Δ
src/glog/logging.h.in 80.00% <ø> (ø)
src/logging.cc 73.85% <69.09%> (-0.83%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@sergiud
Copy link
Collaborator

sergiud commented Aug 4, 2022

Thanks! Unfortunately, Windows unit tests are failing now. Could you please take a look at these first?

@yangzhg
Copy link
Author

yangzhg commented Aug 5, 2022

Thanks! Unfortunately, Windows unit tests are failing now. Could you please take a look at these first?

Ok. I will find a Windows device to test it in this weekend

@yangzhg
Copy link
Author

yangzhg commented Aug 11, 2022

Thanks! Unfortunately, Windows unit tests are failing now. Could you please take a look at these first?

Ok. I will find a Windows device to test it in this weekend

I have tested it on Windows vs 2022 and not failed now, would you please trigger the build again?
image
@sergiud

Add two flags max_logfile_num and log_rolling_policy, support rolling
log file by day or by hour, and use max_logfile_num to control the max
history log file num after rolling.
Copy link
Collaborator

@sergiud sergiud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yangzhg I've rebased your branch. Let's continue from here. Please fetch and reset.

int64 next_flush_time_{0}; // cycle count at which to flush log
WallTime start_time_;
std::multiset<Filetime> file_list_;
Copy link
Collaborator

@sergiud sergiud Oct 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why a std::multiset is needed here. Can a file be associated with multiple timestamps? I don't see how this is possible. Either way, a multiset would be rather memory inefficient here, since the same file name would be potentially stored multiple times.

Please drop the Filetime structure and use an associative container. I guess a std::map is sufficient here.

@sergiud sergiud removed this from the 0.7 milestone Oct 14, 2023
@IceLemon99
Copy link

I just pull the branch code test roll file function, when restart test demo many times the file count will over FLAGS_max_logfile_num. Is this a problem ?

@sergiud
Copy link
Collaborator

sergiud commented Jun 11, 2024

@IceLemon99 I'll close the PR since it's heavily diverged from master. If you want continue working on this please rebase.

@sergiud sergiud closed this Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants