记录设备上的数据并检索日志

在 Xcode 的一个调试版本中,不管我使用的是模拟器还是实际的设备,控制台上都会显示 NSLog、 printf、 fprintf 和 nsAssert 语句

如果我现在在设备上运行一个发布版本(比如说我发送了一个测试飞行版本并在我的 iPhone 上放大; 这将是一个发布版本) ,那么这些(如果有的话)会被记录下来?

那我要怎么找回日志呢?

NSLog 实际上在发布版本构建时输出了一些东西吗?决定因素是什么?是写到 stdout 还是 stderr?是否只有 stderr 写入设备日志?这是否意味着我必须使用 fprintf?设备日志中是否写入了任何内容?有这种东西吗?如果是这样,怎么捡起来呢?

有人能解释一下情况吗?

100715 次浏览

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

If you use Testflight SDK, you can capture all logs with their Remote Logging feature.

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.

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.

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

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.

https://developer.apple.com/library/ios/qa/qa1747/_index.html

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.

In any case, here are the steps:

  1. In Xcode, go to Window -> Devices.
  2. Select your device in the left panel.
  3. Click the little arrow as shown in the screenshot below.

enter image description here

In swift 4.0+, the code of Shyl will changes to,

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

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 to YES 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

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

I think in Xcode 9.3 the device log screen has been moved to a new location.Kindly refer the following link.

Get device logs at runtime in Xcode