Search code examples
iosswiftxcodenslog

Only Crash logs showing in Device Console


Using Xcode 12, and NSLog to log my messages. When I open the "Window -> Devices and Simulators" and I view the log files of my App on my iPhone, I only see Crash logs (crash logs of my and others' apps). Is there a way to see just my regular logging messages? NSLog output is showing in the console, no problem. Thanks....


Solution

  • (Swift 5.3, Xcode 12, iOS 14 — No need for a third party service or library)

    In short: You can replace your NSLog calls with calls to Logger.


    You need to create a Logger object (somewhere, your preference). If you want, you can make your logging easier to filter, e.g. in the Console app, by making various loggers for different parts/functions in your app.

    import os.log
    
    let downloadLogger = Logger(subsystem: "My app", category: "Downloading")
    let somethingLogger = Logger(subsystem: "My app", category: "Lorem ipsum")
    

    Then you call your logger like this:

    // Some error occurs, so we log it:
    downloadLogger.error("Error downloading feed contents: \(error, privacy: .public)")
    
    // Some less important log:
    somethingLogger.info("Secret has been stored: \(mySecret, privacy: .private(mask: .hash))")
    
    

    N.B. our secret is kept secret by applying .private(mask: .hash).

    To view and filter your logs, on the Devices screen, below the View Device Logs you'll find Open Console.

    As the article source states:

    "If you’d like to gather logs from a device, even when your app is not running anymore, you can use the log command on your Mac with the collect option … The logarchive file that you get can then be opened in the Console app and filtered just like live logs can."

    sudo log collect --device --start "2020-06-25 16:10:00" --output myapp.logarchive


    (Credits: source)