Get Capacitor iOS to log to console.app

Solution on how to get Capacitor to log to the iOS Device log / Native log that can be read from console.app. This is a must if you need to debug an approved and published App Store production app.

Problem with Capacitor logging

Capacitor only logs to the Xcode console (or Safari console) but not to the iOS device console (aka the native log), for example from your Capacitor plugins or Cordova plugins, or console.log and console.debug statements in your code.

Having the debug log in Xcode or Safari only is fine when debugging during development or in Testflight. But once your app is published to the AppStore and has some specific error that does not show in dev or Testflight, you need to have log write to the iOS device log, which can be read by console.app on your Mac.

Solution

There is a Capacitor feature request regarding this but if you don’t want to wait for that, this is how to quickly fix it in your project:

  1. Open the file capacitor/ios/Capacitor/Capacitor/CAPLog.swift either from your favourite editor or directly in Xcode.
  2. Comment out the line starting with Swift.print
  3. Instead add the line: NSLog(String(describing: item))
public class CAPLog {
    public static var enableLogging: Bool = true

    public static func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
        if enableLogging {
            for (itemIndex, item) in items.enumerated() {
                //Swift.print("\(item)".prefix(4068), terminator: itemIndex == items.count - 1 ? terminator : separator)
                NSLog(String(describing: item))
            }
        }
    }
}

Now you are ready to build and upload to Apple App Store. Once approved by Apple, start debugging your production build in console.app.

This was tested using Capacitor 3.6.0 / @capacitor/ios 3.6.0 (on an Ionic Framework 6 project)

Note on performance

Swift.print is a more resource effective, but also more limited, way of logging. I doubt that this has much practical impact, but this is something to consider, you may want to revert back to Swift.print after you’re done debugging.

Note on logging

I used NSLog but there are also other ways to use logging that you may want to look into. For example Logger and Apple’s concept of Universal logging / Unified logging system.

Did this solution help you?

I hope this helped! Please comment if it did, or if you had any problem with it. Also let me know if you got it working (or not) using other versions or if you found a better solution – it will help me and others!

Leave a comment

Your email address will not be published. Required fields are marked *