CocoaLumberjack: Fishy behavior using file logger - stops logging after clearing

I’m using two separate file loggers for the purpose of a detailed, debug-level logging and a truncated version for easier on-device diagnostics. The full debug-level log uses an out-of-box file logging experience. This is the one that seems to be exhibiting issues. When I clear the log files (I’ve tried both deletion and rewriting with empty NSData), the logger is unable to write to the file until the application is restarted.

The setup:

 DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
    fileLogger.logFormatter = [[HPDDLogFormatter alloc] init];
    fileLogger.rollingFrequency = 60 * 60 * 24 * 7;
    fileLogger.logFileManager.maximumNumberOfLogFiles = 1;
    self.fileLogger = fileLogger;
    [DDLog addLogger:self.fileLogger withLogLevel:ddLogLevel];

The clear:

    NSArray *paths = [self.appDelegate.fileLogger.logFileManager unsortedLogFileInfos];
    for( DDLogFileInfo *logFileInfo in paths ){
        [[NSFileManager defaultManager] removeItemAtPath:logFileInfo.filePath error:nil];
        [logFileInfo reset];
        DDLogInfo(@"Deleting log file: %@", logFileInfo.filePath);
    }

I’m not sure why this doesn’t work - there aren’t any logs . [edit] Typically I see this after running my -clearLogs method twice in a row. The first time sometimes logging is still there if logged at the time of deletion. The second time is usually good to destroy the files completely. Either way, the truncated log repopulates just fine.


Performing a custom configuration seems to be immune to the above-stated problems:

 // Configure separate trunctaed log file location
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *baseDir = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString *specialLogsDirectory = [baseDir stringByAppendingPathComponent:@"TruncatedLogs"];
    DDLogFileManagerDefault *specialLogFileManager = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:specialLogsDirectory];

    // This is for truncated logging to file. See Live Link-Prefix.pch
    DDFileLogger *specialFileLogger = [[DDFileLogger alloc] initWithLogFileManager:specialLogFileManager];
    specialFileLogger.logFormatter = [[HPDDTruncatedLogFormatter alloc] init];
    specialFileLogger.rollingFrequency = 60 * 60 * 24 * 7; // 24 hour rolling
    specialFileLogger.logFileManager.maximumNumberOfLogFiles = 1;
    specialFileLogger.maximumFileSize = 1024.0f * 256.0f; // 256k max special log size
    self.specialFileLogger = specialFileLogger;
    [DDLog addLogger:self.specialFileLogger withLogLevel:LOG_FLAG_ERROR];

…logging behaves as expected after the log files are deleted.

Any ideas? Am I doing something wrong?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

Ok, I’ll try to investigate more.

Meantime, you can use workaround - archive logfile and then delete it:

[self.fileLogger rollLogFileWithCompletionBlock:^{
    NSArray *paths = [self.fileLogger.logFileManager unsortedLogFileInfos];

    for( DDLogFileInfo *logFileInfo in paths ){

        if (logFileInfo.isArchived) {
            [[NSFileManager defaultManager] removeItemAtPath:logFileInfo.filePath error:nil];
            [logFileInfo reset];
            DDLogInfo(@"Deleting log file: %@", logFileInfo.filePath);
        }
    }
}];

Was there any progress on this issue ? We see similar behaviour when file is supposed to be rolled at the background.