ios 在设备上记录数据并检索日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9097424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logging data on device and retrieving the log
提问by P i
On a debug build in Xcode, regardless of whether I am using the simulator or an actual device, NSLog, printf, fprintf assert and NSAssert statements come out on the console
在 Xcode 中的调试版本中,无论我使用的是模拟器还是实际设备,控制台上都会出现 NSLog、printf、fprintf assert 和 NSAssert 语句
If I now run a release build on the device (say I send a test flight build and big it up on my iPhone; this will be a release build), which of these (if any) are getting recorded?
如果我现在在设备上运行一个发布版本(假设我发送一个试飞版本并在我的 iPhone 上放大它;这将是一个发布版本),其中哪些(如果有)被记录?
And how do I retrieve the log?
以及如何检索日志?
Does NSLog actually output something on release build? What is the determining factor? Whether it is writing to stdout or stderr? is only stderr written to device log? Does this mean I have to use fprintf? Is ANYTHING written to device log? is there even such a thing? If so, how to pick it up?
NSLog 是否真的在发布版本上输出了一些东西?决定因素是什么?是写入标准输出还是标准错误?只有 stderr 写入设备日志吗?这是否意味着我必须使用 fprintf?是否有任何内容写入设备日志?甚至有这样的事情吗?如果有,怎么捡?
Could someone clarify the situation?
有人能澄清一下情况吗?
回答by Myxtic
In Xcode 6.1.1, you can view the NSLog output by doing the following. However, I'm not sure if it lets you see logs from too far back in time. I've only seen it go back up to a couple hours.
在 Xcode 6.1.1 中,您可以通过执行以下操作来查看 NSLog 输出。但是,我不确定它是否可以让您看到太早的日志。我只看到它恢复到几个小时。
In any case, here are the steps:
无论如何,以下是步骤:
- In Xcode, go to Window -> Devices.
- Select your device in the left panel.
- Click the little arrow as shown in the screenshot below.
- 在 Xcode 中,转到窗口 -> 设备。
- 在左侧面板中选择您的设备。
- 单击小箭头,如下面的屏幕截图所示。
回答by shyla
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
Just add this block of code in application:didFinishLaunchingWithOptions
method in the app delegate file and it will create a log file in app document directory on iPhone which logs all console log events. You need to import this file from iTunes to see all console events.
只需application:didFinishLaunchingWithOptions
在应用程序委托文件中的方法中添加此代码块,它就会在 iPhone 上的应用程序文档目录中创建一个日志文件,用于记录所有控制台日志事件。您需要从 iTunes 导入此文件以查看所有控制台事件。
Note: In the .plist file make sure that Application supports iTunes file sharing
is exists and is set to YES
so that you can access through iTunes.
注意:在 .plist 文件中确保Application supports iTunes file sharing
存在并设置为YES
以便您可以通过 iTunes 访问。
To get Logfiles : Launch itunes, after your device has connected select Apps - select your App - in Augument Document you will get your file. You can then save it to your disk
要获取日志文件:启动 iTunes,在您的设备连接后选择应用程序 - 选择您的应用程序 - 在 Augument 文档中,您将获得您的文件。然后您可以将其保存到您的磁盘
回答by Kiran P Nair
In swift 3.0, the code of Shyl will changes to,
在 swift 3.0 中,Shyl 的代码将更改为,
var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(Date()).log"
let logFilePath = (documentsDirectory as NSString).appendingPathComponent(fileName)
freopen(logFilePath.cString(using: String.Encoding.ascii)!, "a+", stderr)
all other process are same that explained by Shyl
所有其他过程与 Shyl 解释的相同
Just add this block of code in application:didFinishLaunchingWithOptions method in the app delegate file and it will create a log file in app document directory on iPhone which logs all console log events. You need to import this file from iTunes to see all console events.
Note: In the .plist file make sure that
Application supports iTunes file sharing
exists and is set toYES
so that you can access through iTunes.To get Logfiles : Launch iTunes, after your device has connected select Apps - select your App - in Augument Document you will get your file. You can then save it to your disk
只需在应用程序委托文件中的 application:didFinishLaunchingWithOptions 方法中添加这段代码,它将在 iPhone 上的应用程序文档目录中创建一个日志文件,该文件记录所有控制台日志事件。您需要从 iTunes 导入此文件以查看所有控制台事件。
注意:在 .plist 文件中确保
Application supports iTunes file sharing
存在并设置为YES
以便您可以通过 iTunes 访问。获取日志文件:启动 iTunes,在您的设备连接后选择应用程序 - 选择您的应用程序 - 在 Augument 文档中,您将获得您的文件。然后您可以将其保存到您的磁盘
回答by Bora
NSLog is written to device log in production release and you can check this by connecting your iPhone to your system and using Organizer. Select your iPhone in the organizer, click Device Logs. You would see all NSLog outputs in the log.
NSLog 被写入生产版本中的设备日志,您可以通过将 iPhone 连接到系统并使用 Organizer 来检查这一点。在管理器中选择您的 iPhone,单击设备日志。您会在日志中看到所有 NSLog 输出。
回答by GrandSteph
I found this link from APPLE very informative and complete. It pretty much gives you all the options to see or access logs of the device whether or not they are connected to your dev machine.
我发现来自 APPLE 的这个链接非常有用且完整。它几乎为您提供了查看或访问设备日志的所有选项,无论它们是否连接到您的开发机器。
https://developer.apple.com/library/ios/qa/qa1747/_index.html
https://developer.apple.com/library/ios/qa/qa1747/_index.html
回答by pojo
If you use Testflight SDK, you can capture all logs with their Remote Logging feature.
如果您使用 Testflight SDK,则可以使用其远程日志记录功能捕获所有日志。
回答by Denis
Yes, NSLog outputs on the device. You can see it's outputs with your device connected to your Mac and using Xcode Organizer tool.
是的,设备上的 NSLog 输出。您可以使用连接到 Mac 的设备并使用 Xcode Organizer 工具查看它的输出。
回答by Ramón Esteban
I know this is an old thread but you can also have access to the device logs going to:
我知道这是一个旧线程,但您也可以访问设备日志:
Settings -> Privacy -> Analytics -> Data
设置 -> 隐私 -> 分析 -> 数据
Hope this help
希望这有帮助
Regards
问候
回答by Vaibhav
I think in Xcode 9.3 the device log screen has been moved to a new location.Kindly refer the following link.
我认为在 Xcode 9.3 中,设备日志屏幕已移至新位置。请参阅以下链接。