Where is the log file stored using cocoaLumberjack

I use the cocoaLumberjack framework to register on iOS. I used this code to store the logs in a file.

DDFileLogger* fileLogger = [[DDFileLogger alloc] init]; fileLogger.rollingFrequency = 60 * 60 * 24; fileLogger.logFileManager.maximumNumberOfLogFiles = 7; [DDLog addLogger:fileLogger]; DDLogVerbose(@"hello"); NSLog(@"hihihihihi"); 

I can’t find exactly where the log file generated by this code is stored. Can someone help me with this problem?

+45
ios logging objective-c lumberjack ddfilelogger
Jun 20 2018-11-11T00:
source share
7 answers

Got an answer

It is stored in Library / Application Support / Iphone Simulator / # version no # / applications / # your application # / documents / logs / log-3hex no>

+6
Jun 21 2018-11-11T00:
source share

The answers here do not say that there may be multiple log files. The logFileManager property of the DDFileLogger instance can be used to record file information. Check out DDFileLogger.h for public methods and properties. The following may be useful:

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

Here is my solution for receiving log data (and sending via email). Note that the default number of log files is 5 by default.

 - (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]; } } 
+60
Jul 18 2018-12-18T00:
source share

You can download log files from a connected device or send them directly from the application. Both approaches are described below.

Send log files from the application via email to Swift

Write this in a class where you have a link to DDFileLogger. I would put this in a custom registrar class, for example. MyLogger.swift

 var ddFileLogger: DDFileLogger! var logFileDataArray: [NSData] { get { let logFilePaths = ddFileLogger.logFileManager.sortedLogFilePaths() as! [String] var logFileDataArray = [NSData]() for logFilePath in logFilePaths { let fileURL = NSURL(fileURLWithPath: logFilePath) if let logFileData = try? NSData(contentsOfURL: fileURL, options: NSDataReadingOptions.DataReadingMappedIfSafe) { // Insert at front to reverse the order, so that oldest logs appear first. logFileDataArray.insert(logFileData, atIndex: 0) } } return logFileDataArray } } 

Then, when the user clicks a button to indicate that they want to send logs,

 // Required by MFMailComposeViewController import MessageUI @IBAction func writeEmailTapped(sender: AnyObject) { if MFMailComposeViewController.canSendMail() { let composeVC = MFMailComposeViewController() composeVC.mailComposeDelegate = self // Configure the fields of the interface. composeVC.setToRecipients(["your-email@company.com"]) composeVC.setSubject("Feedback for app") composeVC.setMessageBody("", isHTML: false) let attachmentData = NSMutableData() for logFileData in MyLogger.sharedInstance.logFileDataArray { attachmentData.appendData(logFileData) } composeVC.addAttachmentData(attachmentData, mimeType: "text/plain", fileName: "diagnostic.log") self.presentViewController(composeVC, animated: true, completion: nil) } else { // Tell user about not able to send email directly. } } 

This creates an email popup with an attachment file named diagnostic.log , which is all the log files combined together.

Special thanks are almost a translation of Swift from the Objective-C version given by another answer.

Get the log file from the device directly, via a USB cable

If you want to get the log files created by your application on the device,

  • Connect your device to Mac.
  • In Xcode, go to Window -> Devices
  • In the upper left corner of the list of devices, click on the connected device.
  • In the main panel, under the "Installed applications" section, click the application in which you launched CocoaLumberjack.
  • At the bottom of the Installed Applications list, click the gear icon and then Download Container.
  • In Finder, right-click (show menu) in the saved .xcappdata file and select "Show Package Contents"
  • Log files are saved in /AppData/Library/Caches/Logs/

Pausing the vote would be nice if it were useful to you!

+44
Dec 22 '12 at 5:33
source share

If you use CocoaLumberjack, you have DDFileLogger.h , and you can set the already implemented method currentLogFileInfo: ::

 @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]); 

What, on my iPhone, is printed:

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

for both the console and the file.

+26
Jul 10 '12 at 19:45
source share

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

 NSString * applicationDocumentsDirectory = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path]; DDLogFileManagerDefault *documentsFileManager = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:applicationDocumentsDirectory]; DDFileLogger *fileLogger = [[DDFileLogger alloc] initWithLogFileManager:documentsFileManager]; 
+15
Sep 28 '13 at 9:27
source share

Found this to be the last:

 DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; fileLogger.logFileManager.logsDirectory;//THIS 

From the official code:

The default log file manager.

All log files are placed in the logsDirectory directory. If no specific logsDirectory is specified, the default directory is used. On a Mac, this is in ~ / Library / Logs /. On the iPhone, this is in ~ / Library / Caches / Logs. The log files are called "log-.txt", where uuid is a hexadecimal hexadecimal character consisting of the set [0123456789ABCDEF]. Archived log files are automatically deleted in accordance with the maximumNumberOfLogFiles property.

+8
Feb 18 '14 at 7:20
source share

Send log files from an application via email, Objective-C

Objective-C code:

In the title:

 #import <MessageUI/MessageUI.h> #import "DDLog.h" #import "DDFileLogger.h" 

In your implementation:

 - (NSMutableArray *)errorLogData { DDFileLogger *ddFileLogger = [DDFileLogger new]; NSArray <NSString *> *logFilePaths = [ddFileLogger.logFileManager sortedLogFilePaths]; NSMutableArray <NSData *> *logFileDataArray = [NSMutableArray new]; for (NSString* logFilePath in logFilePaths) { NSURL *fileUrl = [NSURL fileURLWithPath:logFilePath]; NSData *logFileData = [NSData dataWithContentsOfURL:fileUrl options:NSDataReadingMappedIfSafe error:nil]; if (logFileData) { [logFileDataArray insertObject:logFileData atIndex:0]; } } return logFileDataArray; } - (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:@"filename.log"]; [mailViewController setSubject:NSLocalizedString(@"LogFile Subject", @"")]; [mailViewController setToRecipients:[NSArray arrayWithObject:@"email@email.com"]]; [self presentViewController:mailViewController animated:YES completion:nil]; } 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]; } } - (void)mailComposeController:(MFMailComposeViewController *)mailer didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { [self becomeFirstResponder]; [mailer dismissViewControllerAnimated:YES completion:nil]; } 

Here's how you could add something to your log file:

 DDLogError(@"This is an error."); DDLogWarn(@"This is a warning."); DDLogInfo(@"This is just a message."); DDLogVerbose(@"This is a verbose message."); 

Remember to install ddLogLevel in the Constants file.

Constants.h:

 extern NSUInteger const ddLogLevel; 

Comstants.m:

 NSUInteger const ddLogLevel = #ifdef DEBUG LOG_LEVEL_VERBOSE; #else LOG_LEVEL_ERROR; #endif 

This is very obvious, but remember to configure CocoaLumberjack in the AppDelegate.m file.

 [DDLog addLogger:[DDASLLogger sharedInstance]]; [DDLog addLogger:[DDTTYLogger sharedInstance]]; DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; [fileLogger setMaximumFileSize:(1024 * 1024)]; [fileLogger setRollingFrequency:(3600.0 * 24.0)]; [[fileLogger logFileManager] setMaximumNumberOfLogFiles:7]; [DDLog addLogger:fileLogger]; 

The best place to paste this code is - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; in the file AppDelegate.m . You should also add this line of code to the AppDelegate.m header:

 #import <CocoaLumberjack/CocoaLumberjack.h> 

Hope this helps.

+4
04 Oct '16 at 2:53 on
source share



All Articles