我正在使用cocoaLumberjack日志(log)框架进行iOS日志(log)记录.

DDFileLogger* fileLogger = [[DDFileLogger alloc] init];
fileLogger.rollingFrequency = 60 * 60 * 24; 
fileLogger.logFileManager.maximumNumberOfLogFiles = 7;

[DDLog addLogger:fileLogger];

DDLogVerbose(@"hello");
NSLog(@"hihihihihi");

我找不到这个代码生成的日志(log)文件存储在哪里.

推荐答案

您可以从连接的设备下载日志(log)文件,也可以直接从应用程序发送.下面介绍这两种方法.

Send log files from app through email, in Swift

在引用DDFileLogger的类中编写此代码.我会把它放在一个定制的记录器类中,例如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
    }
}

然后,当用户点击一个按钮表示他们想要发送日志(log)时,

// 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.
    }
}

这将导致出现一个" compose 邮箱"弹出窗口,其中包含一个名为diagnostic.log的附件文件,即所有连接在一起的日志(log)文件.

特别感谢——这是另一个答案给出的Objective-C版本的快速翻译.

Get log file(s) from device directly, through USB cable

如果您想获取应用程序在设备上运行时创建的日志(log)文件,

  1. 将设备连接到mac电脑
  2. In Xcode, go to Window -> Devices
  3. 在设备列表的左上角,单击连接的设备.
  4. 在主面板的"已安装的应用程序"部分中,单击运行CocoaLumberjack的应用程序.
  5. 在已安装应用列表的底部,单击gear图标,然后下载容器.
  6. 在Finder中,右键单击保存的屏幕上的(显示菜单).xcappdata文件,然后 Select "显示软件包内容"
  7. 日志(log)文件保存在/AppData/Library/Caches/Logs/

如果这对你有帮助的话,向上投票会很好!

Objective-c相关问答推荐

如何在 Objective-C 中编写计时器?

Objective C 中的多个委托

Objective C 布尔数组

如何追踪 SIGABRT 的原因

在objective-c中isa是什么意思?

iOS 心率检测算法

判断正在使用的应用程序版本

烦人的[Environment: Sandbox]提示

使用对象引用的 NSArray,我是显式释放数组中的所有对象还是仅释放数组本身?

适用于 ios 的 360° 全景库

以编程方式创建 UITableView

Apple 的 API 中的k前缀表示什么?

未知类型名称类;您指的是 'Class' 吗?

如何删除应用程序可访问的所有 keys 串项目?

Cocoa 与 Cocoa Touch - 有什么区别?

判断是否已实现可选协议方法

Objective-C中的继承和类别有什么区别

[NSMutableArray array] 与 [[NSMutableArray alloc] init] 之间的区别

创建 NSTextField标签的示例代码?

允许用户从 UILabel 中 Select 文本进行复制