Where is Logfile stored using cocoaLumberjack

前端 未结 9 984
不知归路
不知归路 2020-12-22 18:07

I am Using cocoaLumberjack logging framework for iOS logging. For storing logs in a file I used this code.

DDFileLogger* fileLogger = [[DDFileLogger alloc] i         


        
相关标签:
9条回答
  • 2020-12-22 18:56

    If you're using CocoaLumberjack, you have DDFileLogger.h and you can expose the currentLogFileInfo: method that is implemented already:

    @interface DDFileLogger : DDAbstractLogger <DDLogger>
    ...
    - (DDLogFileInfo *)currentLogFileInfo;
    @end
    

    Then, you can programmatically access the path to the current file with:

    // configure logger
    DDFileLogger *fileLogger = [DDFileLogger new];
    [DDLog addLogger:fileLogger];
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
    DDLogInfo(@"log file at: %@", [[fileLogger currentLogFileInfo] filePath]);
    

    Which, on my iPhone, printed:

    log file at: /var/mobile/Applications/3BE1219F-78BE-491C-B68C-74D6FA0C2EF1/Library/Caches/Logs/log-5D1286.txt
    

    to both the console and the file.

    0 讨论(0)
  • 2020-12-22 18:56

    You can control where it is stored, for example, I sometime store it in the iTunes folder for easy retrieval. Use this in the AppDelegate when setting up the fileLogger:

    NSString * applicationDocumentsDirectory = [[[[NSFileManager defaultManager]    
         URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path];
    DDLogFileManagerDefault *documentsFileManager = [[DDLogFileManagerDefault alloc]
         initWithLogsDirectory:applicationDocumentsDirectory];
    DDFileLogger *fileLogger = [[DDFileLogger alloc]
         initWithLogFileManager:documentsFileManager];
    
    0 讨论(0)
  • 2020-12-22 18:57

    The answers here don't seem to account for the fact that there may be multiple log files. You can use your DDFileLogger instance's logFileManager property to loop through file information. Check out DDFileLogger.h for public methods and properties. The following may be of use:

    - (NSString *)logsDirectory;
    
    - (NSArray *)unsortedLogFilePaths;
    - (NSArray *)unsortedLogFileNames;
    - (NSArray *)unsortedLogFileInfos;
    
    - (NSArray *)sortedLogFilePaths;
    - (NSArray *)sortedLogFileNames;
    - (NSArray *)sortedLogFileInfos;
    

    Here is my solution for getting log data (and emailing it). Note that the default number of log files is 5 as of this writing.

    - (NSMutableArray *)errorLogData {
        NSUInteger maximumLogFilesToReturn = MIN([KRLogManager sharedInstance].fileLogger.logFileManager.maximumNumberOfLogFiles, 10);
        NSMutableArray *errorLogFiles = [NSMutableArray arrayWithCapacity:maximumLogFilesToReturn];
        DDFileLogger *logger = [KRLogManager sharedInstance].fileLogger;
        NSArray *sortedLogFileInfos = [logger.logFileManager sortedLogFileInfos];
        for (int i = 0; i < MIN(sortedLogFileInfos.count, maximumLogFilesToReturn); i++) {
            DDLogFileInfo *logFileInfo = [sortedLogFileInfos objectAtIndex:i];
            NSData *fileData = [NSData dataWithContentsOfFile:logFileInfo.filePath];
            [errorLogFiles addObject:fileData];
        }
        return errorLogFiles;
    }
    
    - (void)composeEmailWithDebugAttachment {
        if ([MFMailComposeViewController canSendMail]) {
    
            MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
            mailViewController.mailComposeDelegate = self;
            NSMutableData *errorLogData = [NSMutableData data];
            for (NSData *errorLogFileData in [self errorLogData]) {
                [errorLogData appendData:errorLogFileData];
            }
            [mailViewController addAttachmentData:errorLogData mimeType:@"text/plain" fileName:@"errorLog.txt"];
            [mailViewController setSubject:NSLocalizedString(@"Good Subject", @"")];
            [mailViewController setToRecipients:[NSArray arrayWithObject:@"some@email.com"]];
    
            [self presentModalViewController:mailViewController animated:YES];
    
        }
    
        else {
            NSString *message = NSLocalizedString(@"Sorry, your issue can't be reported right now. This is most likely because no mail accounts are set up on your mobile device.", @"");
            [[[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles: nil] show];
        }
    }
    
    0 讨论(0)
提交回复
热议问题