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

Log rotate has some problem in 3.7.4 version #1276

Closed
oyljerry opened this issue Jan 4, 2022 · 15 comments
Closed

Log rotate has some problem in 3.7.4 version #1276

oyljerry opened this issue Jan 4, 2022 · 15 comments

Comments

@oyljerry
Copy link

oyljerry commented Jan 4, 2022

Issue Info

Info Value
Platform Name osx
Platform Version all
CocoaLumberjack Version 3.7.4
Integration Method carthage
Xcode Version Xcode 12.4
Repro rate all the time (100%)

Issue Description and Steps

I have just upgraded CocoaLumberjack from 3.7.2 to 3.7.4, But I found there is some strange problems occur.

Below is code about how to control log rotate.

    fileLogger.maximumFileSize = 20 * 1024 * 1024;
    fileLogger.rollingFrequency = 0;

image

Issuer#1:

I found that the log is rotated when its size is only 1.3M, In 3.7.2, This works as 20M.

Issuer#2:

The log file which should be xxx-202112291336.log, but it has been changed to xxx-202212291336.log

Is this issue related to #1234???

@ffried
Copy link
Member

ffried commented Jan 4, 2022

Are you using a subclass of DDLogFileManagerDefault?
If so, I could see both issues coming from an incomplete implementation:

The default log file name uses the following pattern as log file name: " .log"
Since your format differs from that, you've likely overridden -newLogFileName. To solve your date issue, please have a look at the date formatter you are using there. Specifically take care to use yyyy not YYYY for the year.

Also, if you override -newLogFileName, you also need to properly implement -isLogFile:! Otherwise the log files won't be recognized by other (non-overridden) implementations of DDLogFileManagerDefault and thus might not be reused correctly, resulting in new log files even though the criteria for rolling hasn't yet been met.

@oyljerry
Copy link
Author

oyljerry commented Jan 6, 2022

Thanks @ffried , The date format is wrong, I need to change from YYYY to yyyy.

@oyljerry oyljerry closed this as completed Jan 6, 2022
@oyljerry oyljerry reopened this Jan 12, 2022
@oyljerry
Copy link
Author

oyljerry commented Jan 12, 2022

@ffried :
After some testing, We found issuer1 still happened in below scenario (in 3.7.2, We do not found this issue). Please help to check it.

  1. On 2022-01-06, start app, the log generated date is aaaa-202201061900.log,
  2. I shutdown my macOS at 2022-01-06 22:00
  3. After 1 day, at 2022-01-07, I power on my macOS, start my app.
  4. It will generate a new log, aaaa-202201071416.log, however, aaaa-202201061900.log size is 1.4MB
  5. I have set log module properties as below.
fileLogger.maximumFileSize = 20 * 1024 * 1024;
fileLogger.rollingFrequency = 0;
[[fileLogger logFileManager] setMaximumNumberOfLogFiles:5];
[[fileLogger logFileManager] setLogFilesDiskQuota:0];

Below is newLogFileName and isLogFile implementation.

- (NSString *)newLogFileName
{
    static dispatch_once_t onceToken;
    static NSDateFormatter *dateFormatter;
    dispatch_once(&onceToken, ^{
        dateFormatter = [NSDateFormatter new];
        [dateFormatter setDateFormat:@"yyyyMMddHHmm"];
    });
    NSString* date = [dateFormatter stringFromDate:NSDate.date];
    return [NSString stringWithFormat:@"%@-%@.log", @"aaaa", date];
}

- (BOOL)isLogFile:(NSString *)fileName
{
    NSLog(@"fileName = %@, %@", fileName, @"aaaa");
    return [fileName hasPrefix:@"aaaa"];
}

PS. This issue will not happen, If I do not restart my app, ex, I keep running my app from 20220107 to 20220110, It will generate three log files. The log module works as expectation.
aaaa-202201070207.log -> 21M
aaaa-202201081939.log -> 21M
aaaa-202201101306.log -> 6.6M

@ffried ffried mentioned this issue Feb 16, 2022
8 tasks
@ffried
Copy link
Member

ffried commented Feb 18, 2022

@oyljerry Can you try with the lastest state of master? There has been a change made in #1287 that might resolve that problem.

@coolwuzizai
Copy link

@ffried hi,I meet this problem. when I use pod 'CocoaLumberjack/Swift', '3.7.0',the swift project can rotate after 24 hours. But pod 'CocoaLumberjack', '3.7.2' or '3.7.4',the objective c project cann't rotate after 24 hours. I don't know why. I just guess maybe dispatch_time? or different between dispatch_time and dispatch_walltime ? Could you give me some help or suggestion? thanks.

@coolwuzizai
Copy link

I do like oyljerry, but
fileLogger.maximumFileSize = 0; fileLogger.rollingFrequency = 60 * 60 * 24,he rorate log by filesize,I want to rorate by hours. newLogFileName and isLogFile implementation also like him.

@coolwuzizai
Copy link

@oyljerry hi ,Could you give some advices or tell me how you fix it ?thank you very much.

@ffried
Copy link
Member

ffried commented Aug 9, 2022

@oyljerry @coolwuzizai I just went through all the changes since 3.7.0 and I really can't seem to figure out what's wrong.
I've just pushed a small change (cf6e7e0) that might fix some of this issue, but I'm not sure it's the root of the evil.
I'd still be very happy to have a small reproduction project on my hands - best with a small rolling interval to easily reproduce it w/o having to wait a day 😉

Could you guys give master a test (with my latest commit) to see if anything changes?

@coolwuzizai
Copy link

@ffried thanks,I will test for the master. oh,I remember something,when I set fileLogger.rollingFrequency = 60 * 3 (test for three minutes or ten minutes,it works ! But when set 60 * 60 * 24, cann't rotate ,so I guess the dispatch_time because dispatch_time will stop when computer is sleep). But if I restart the app after rollingFrequency, it sometimes create a new LogFile.

@coolwuzizai
Copy link

@ffried hi,I find a problem 16 days ago the old version. I set fileLogger.maximumFileSize = 50 * 1024 * 1024;
fileLogger.rollingFrequency = 0; But it will roll about 25 hours. I know this could because dispatch_time, but why this Log rotate use 24hours not maximumFileSize. So fantasy! I will load the new code to see this if I have time, thanks

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contribution.

@github-actions github-actions bot added the Stale label Aug 24, 2023
@OrrHsiao
Copy link

OrrHsiao commented Aug 29, 2023

I found the same problem with version 3.8.1,this is my configuration,but in reality, every time you reopen the app, it generates a new file.

DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
fileLogger.doNotReuseLogFiles = NO;
fileLogger.rollingFrequency = 60 * 60 * 24;
fileLogger.maximumFileSize = 30 * 1024 * 1024;
fileLogger.logFileManager.maximumNumberOfLogFiles = 10;
fileLogger.logFileManager.logFilesDiskQuota = 1024 * 1024 * 500;
[fileLogger setLogFormatter:self];
[DDLog addLogger:fileLogger withLevel:level];

@github-actions github-actions bot removed the Stale label Aug 29, 2023
@ffried
Copy link
Member

ffried commented Aug 29, 2023

Can you guys try the latest master branch? #1357 is another candidate that might fix this.

@OrrHsiao
Copy link

Can you guys try the latest master branch? #1357 is another candidate that might fix this.

I use the latest master code and this problem no longer occurs, thanks😊

@ffried
Copy link
Member

ffried commented Aug 29, 2023

Thanks for confirming. Then this is finally fixed.

@ffried ffried closed this as completed Aug 29, 2023
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

No branches or pull requests

4 participants