92

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

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?

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?

Could someone clarify the situation?

Yahia
  • 69,653
  • 9
  • 115
  • 144
P i
  • 29,020
  • 36
  • 159
  • 267
  • 1
    For just save the outputs as File use this: http://stackoverflow.com/questions/28114110/possible-to-write-swift-println-logs-into-file-too/41740777#41740777 – Kiran P Nair Jan 19 '17 at 11:54

9 Answers9

141

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

Myxtic
  • 5,679
  • 5
  • 48
  • 69
  • 3
    This should be the correct answer... I spent half an hour to look for the log button. Thanks for saving my time... – PrimaryChicken Feb 16 '15 at 07:39
  • 1
    just NSLog. Don't expect cout or printf there. – kiranpradeep Apr 24 '15 at 14:26
  • 11
    You cannot get the logs from production/submitted apps using this technique. Its useful only while debugging. – Satyam Jun 02 '15 at 06:22
  • 7
    you made my day, perfect example how NOT to design UI maybe they should read https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/OSXHIGuidelines/ – Hofi Jun 18 '15 at 17:39
  • 1
    You just saved my bacon. Have been chasing a bug that only appears in release version, logs revealed the issue. I had looked at that same dialog and never thought to click the small arrow, which is apparently a universal symbol for "show live device logs". Thank you! – chacmool Jul 26 '15 at 22:31
  • @Satyam I don't think that's necessarily true, beyond it being common practice to remove the NSLogs in production builds with preprocessor definitions and macros (`#ifndef DEBUG`). But if they are left in I believe you can see them from here. – WiseOldDuck Dec 08 '15 at 17:53
  • Does this work for print using swift2? I'm trying to output something in Swift and don't see a thing. – sonoluminescence Mar 17 '16 at 00:23
  • 1
    This answer may have been good back then but it looks like it now only works for current log output (since the iphone was plugged in). I can't see how to get historic logs from before that – wheeliebin Dec 15 '18 at 16:27
100
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

shyla
  • 1,430
  • 1
  • 13
  • 10
  • 1
    How does this work? just by having a `.log` file in the documents directory iOS writes your console output there? – Ali May 18 '16 at 10:27
  • 2
    How to delete log files programmatically by date order to prevent leak disk memory? – hkaraoglu Nov 24 '16 at 11:22
  • 4
    Not sure if `NSDocumentDirectory` is appropriate for log files - it is usually meant for user's own generated content, but log files are app support content, therefore maybe they belong in `NSLibraryDirectory` /Logs subdirectory. – JustAMartin Jan 26 '17 at 20:24
  • How can I export the log details from windows PC ? – Vineesh TP Feb 09 '17 at 09:02
  • 2
    @Ali it works by associating the stderr (standard error output) file descriptor (i.e. file descriptor 2) to a file open for writing. So every time you print to stderr, it prints to the file instead. That way, you catch the stderr output of the application. The log system knows nothing about it. – Ammo Goettsch Feb 20 '17 at 23:44
  • @AmmoGoettsch will it work in release mode as well ? – Priyal Mar 06 '17 at 11:40
  • @Priyal Yes, it is just regular code. It does the same regardless of debug vs release builds. Use with caution so you don't create big files on your released app's file system! – Ammo Goettsch Nov 24 '17 at 16:38
  • @shyla I'm wondering what I need to do to have this work for Watch Extension? – user2623825 Feb 02 '18 at 20:49
  • In regards to my above comment: This is actually not possible, as seen here: https://forums.developer.apple.com/thread/11918 Instead what I ended up doing was using WCSession to send a message back to the iPhone application code. Then, when this message was received, I use NSLog and the solution proposed by Shyla. This allows the messages from Watch Extension to show up in iTunes along with NSLog from the rest of the iPhone application. – user2623825 Feb 03 '18 at 01:40
  • @shyla - can I do this for print() statements as well or just NSLog? – Fra Jul 25 '19 at 17:21
  • How to get this file programmatically in the application? so users can send logs to admin? – Shahzain ali Nov 29 '21 at 16:01
27

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

Kiran P Nair
  • 2,041
  • 22
  • 31
  • correct me if I'm wrong: This **changes** the directory of the logs meaning it's originally written in `/var/db/diagnostics/`. See [here](https://stackoverflow.com/questions/45353818/how-can-i-open-view-logs/45356093?noredirect=1#comment77676493_45356093). – mfaani Jul 27 '17 at 19:02
  • @Honey , this log will saved in the app document directory. – Kiran P Nair Jul 28 '17 at 07:08
  • This code always create a new file when app launched – DURGESH Nov 23 '17 at 10:55
  • Any idea how to get this to work when the log is executed within a Watch Extension? – user2623825 Feb 02 '18 at 20:49
  • 1
    @KiranPNair - can I do this for print() statements as well or just NSLog? – Fra Jul 25 '19 at 17:22
12

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.

Bora
  • 417
  • 3
  • 15
  • 4
    Update: In Xcode 6 it is no longer possible to read the NSLog outputs via the Device Logs. Window > Organizer no longer lists the devices. Instead Window > Devices lists the devices, but only displays the crash logs. – Robert Nov 13 '14 at 18:29
  • 1
    Robert, on Window > Devices screen of Xcode 6 there is an invisible line near the bottom edge of the screen. If you pull it up you'll see the console log of your selected device. It is invisible because of an UI bug in Xcode I think. – oradyvan Dec 04 '14 at 14:01
7

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

GrandSteph
  • 2,053
  • 1
  • 16
  • 23
2

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

Ramón Esteban
  • 916
  • 7
  • 10
2

Yes, NSLog outputs on the device. You can see it's outputs with your device connected to your Mac and using Xcode Organizer tool.

Denis
  • 6,313
  • 1
  • 28
  • 27
  • Is it possible for a user to inspect these logs through iTunes? – P i Feb 01 '12 at 15:19
  • 3
    This answer http://stackoverflow.com/questions/2334664/can-i-get-console-logs-from-my-iphone-app-testers is stating iPhone Configuration Utility is possible to export console log from device – Denis Feb 01 '12 at 15:23
2

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

russau
  • 8,928
  • 6
  • 39
  • 49
pojo
  • 5,892
  • 9
  • 35
  • 47
0

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

Vaibhav
  • 865
  • 2
  • 10
  • 19