Get Debug.Log output in the working iOS build

I’ve got a question.

I have an application that is uploaded to AppStore. And I have a device (iPhone, jailbroken) with this app installed. Is there any way to see what “Debug.Log” is outputting in the build?

I’ve found only way to see the console through “iPhone Configuration Utility”, but there are only some system outputs in that console.

Is there any chance to see something similar to what I see when I build a project directly to my phone and then look at xCode’s “all output” ?

You can use log callbacks to save log to file, or send it to your server.

public class LogHandler : MonoBehaviour
{
    private StreamWriter _writer;
	void Awake()
    {
        _writer = File.AppendText("log.txt");    
        _writer.Write("

=============== Game started ================

");
DontDestroyOnLoad(gameObject);
Application.RegisterLogCallback(HandleLog);
}

    private void HandleLog(string condition, string stackTrace, LogType type)
    {
        var logEntry = string.Format("

{0} {1}
{2}
{3}"
, DateTime.Now, type, condition, stackTrace);
MyServer.SendLog(logEntry);
_writer.Write(logEntry);
}

    void OnDestroy()
    {
        _writer.Close();
    }
}

Hey so – good answer by @Mozgold but – well, first off let me note that Application.RegisterLogCallback has now been replaced – any one looking to use @Mozgold’s answer should use Application.logMessageReceivedThreaded or Application.logMessageReceived – see this thread How to use Application.logMessageReceived for logging ? - Questions & Answers - Unity Discussions

I had the same problem, but preferred instead to use a bridge to pipe Debug.Log messages to NSLog. The downside of this approach is that, in XCode’s console, you see the messages twice. The advantage is that, when running a TestFlight’d build, I see the messages live as they happen in XCode’s Window / Devices log when the app is running on my selected device.

STEP 1 – register a callback so that Debug.Log’s are piped to method in my iOS plugin

	public void OnEnable() {
		// Debug.LogWarning("------ ------ Registered log callback----- ---------");
		Application.logMessageReceived += IOSPlugin.LogToiOS;
	}

	public void OnDisable() {
		Application.logMessageReceived -= IOSPlugin.LogToiOS;
	}

STEP 2 – implement method in my iOSPlugin to pipe these calls to the native app

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class IOSPlugin : MonoBehaviour {

	#if UNITY_IPHONE
	[DllImport ("__Internal")]
	private static extern void _logToiOS(string debugMessage);
	#endif

	public static void LogToiOS(string logString, string stackTrace, LogType type) {
		// We check for UNITY_IPHONE again so we don't try this if it isn't iOS platform.
		#if UNITY_IPHONE
		// Now we check that it's actually an iOS device/simulator, not the Unity Player. You only get plugins on the actual device or iOS Simulator.
		if (Application.platform == RuntimePlatform.IPhonePlayer) {
			_logToiOS(logString + "

===============
" + stackTrace);
}
#endif
}
}

STEP 3 – implement a method in my iOS app’s AppController.mm to pipe this to NSLog – you should be able to just directly drop this in your AppController.mm and be all set

extern "C" {
  
    void _logToiOS(const char* debugMessage) {
 		NSLog(@"Received _logToiOS %@", [NSString stringWithUTF8String:debugMessage]);
    }

}

First build your iOS app with “Development Build” enabled. In Unity 4, Debug.Log() output is shown in the XCode’s debug Console. Notice that this is NOT the Console in the Organizer, which shows the device events, but it’s the Console that is shown at the bottom of the main window, which is called Debug area. The Organizer’s Device Logs is useful to obtain the crash logs when the app is executing out of XCode.

You can check that with another option. Connect you phone to your MAC which have xCode install. Now open xCode and from xCode open Organizer. From Organizer select Devicestab and in that you can see you device in that. And from your selected device select Console in that you can see the device log.

You can redirect NSLog (and so, what you write with Debug.Log, will also be redirected) to file adding something like that to your native ios code:

- (void)redirectLogToDocument
{
     NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [allPaths objectAtIndex:0];
     NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:@"yourFile.txt"];

     NSLog(@"redirectLogToDocument: %@", pathForLog);
    
     freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
    freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stdout);
}

// usage:

    if (!isatty(STDERR_FILENO)) { // if debugger not attached
        [self redirectLogToDocument]; // redirect logs
    }

Then you can take your file using Window → Devices & Simulators → select your device → select your app → press settings button below → select “download container” from dropdown. xcappdata file will be saved. Reveil in finder, click right mouse button, click “show package contents”, then go to Documents folder. There will be your file.

Screenshot better describes that:

Based on: