Swift, iOS 14: where to find device logs? - ios

I am trying to debug my app and I need to log some info. Here is my code:
let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "main")
logger.log("One")
logger.notice("Two")
os_log("Three")
But when I press Cmd-Shift-2 -> View Device Logs after that, it doesn't show any logs related to my app. I want to read these messages somewhere - how do I do that?

You should be able to use the macOS console.app to see your logs.
Additional information » Exploring logging in Swift: https://developer.apple.com/videos/play/wwdc2020/10168/

Related

How to use Console.app to view debug-level logs from os_log from an iOS app running in the Simulator?

This is a question about how to use Apple's Unified Logging framework, also known as "os_log", the function you call from Swift.
Specifically, I want to know if it is possible:
to view logs in Console.app (not with the log command line tool) ...
to see debug-level logs (not just info-level logs) ...
for an iOS app running in the Simulator (not just running on a device).
You cannot do this just by running Console.app, pointing at the Simulator, and selecting Action / Include Debug Message, which is the obvious thing to do.
This article suggests that debug-level logs don't show up because "the 'system' log level is set to info" but I can't find a discussion anywhere that explains what that means.
This appears to be a bug in the Console app when dealing with the simulator: https://forums.developer.apple.com/thread/82736
I believe a Radar has been logged: rdar://47667447

Why crash log couldn't be found

I have an app and under some circumstance the app crashes.It has crashed couple of times.But I am not able to find the crash log.
I opened Xcode -> Window -> Devices & Simulators ->View Device logs - The device
I am not seeing any crash logs over there for the Time it crashed.
Any ideas please
It has happened to me before.
Just delete the old crash logs and let the crash happen and you can easily find the crash log.
I would suggest you to use the following option to check for the crash logs in iOS device itself as its easy and quick to do.(depending on the OS version the below selection process will change)
Settings -> Privacy -> Diagnostic & Usage -> Diagnostic and Usage Data
Just connect the iOS device to a machine and sync it with iTunes then all the crash logs that you saw in Diagnostic and usage Data will not be there any more and it'll be in the machine.
Next time when the crash happens you can see the crash log
under Diagnostic and Usage Data
you can click on the any particular crash log and share it via mail
you can sync it with iTunes and find it in OSx at ~/Library/Logs/CrashReporter/MobileDevice/
you can go to Xcode->Devices and Simulators -> View Device Logs

Viewing os_log messages in device console

I'm trying to get some logging out of my app through Unified Logging (os_log)
Here's the initialization of the log:
var osLog : OSLog = OSLog(subsystem: "com.test.testapp", category: "native-tester")
And here's how I use it:
os_log("iOS App initialized successfully!", log: osLog, type:.info)
When debugging the app normally, the logs appear properly on the console output, but when I look at the device console (Shown in the "Devices and Simulators" window) I don't see them at all.
This article says that you should configure the system to enable the debug logs using
sudo log config --mode "level:debug" --subsystem com.test.testapp
But that didn't seem to make a difference. I'm guessing it's because I'm configuring the mac to view the logs, not the iPad.
How do I view the ipad / iphone logs from os_log in the device console?
The "Devices and Simulators" window only shows crash reports. Use the Console app or Terminal, via the log --stream command, to see live log output.
To see the device's live log messages via the Console app, either when running from Xcode or when running from the device directly:
Open Console.app.
Click on the device's name in the left side panel, under "Devices".
Select Action, then Include Info Messages from the menu. If you are also using .debug level messages, make sure to select Include Debug Messages as well. (Without those items selected, the Console displays .default, .fault, and .error level messages only.)
If you still don't see the messages, try entering this Terminal command to configure the logging levels for your app:
sudo log config --subsystem com.test.testapp --mode level:debug
This turns on .debug-level logging for subsystem "com.test.testapp" (which includes .info and .default messages).
If you want to persist the messages, rather than the default of memory-only, turn on persistence for the three levels at the same time, like so:
sudo log config --subsystem com.test.testapp --mode level:debug,persist:debug
Regardless of any log settings, though, only crash reports will appear in the "Devices and Simulators" window.
Another pitfall: If you have set OS_ACTIVITY_MODE to disable in your scheme, then you will not see any logs for your app in console.
You have to remove or uncheck that argument.
Log types .debug and .info are by default memory only (not saved on disk) so it won't be visible on the device console.
Detailed info:
https://developer.apple.com/documentation/os/logging?language=objc
Also here is pretty nice WWDC:
https://developer.apple.com/videos/play/wwdc2016/721/
In the console app, there are two options to include / hide debug and info messages:
Following screenshots could be helpful for reference-
My os_log went missing after releasing an app to app store. I guess when I built the app for release, the debug option was turned off.
To enable the logging back... go to Product > Scheme > Edit Scheme > Run Debug ... then check Debug executable.

Change the log level on iOS 10 (Unified Logging) with Console.app

This is so simple but: how on earth do I set the level of log messages I see in Console.app, if I am trying to use iOS10's new "Unified Logging & Activity Tracing" API?
In other words, if I have code running on iOS like so:
fileprivate let logger = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "mycategory")
fileprivate func logv(_ s:String) {
os_log("%#",log:logger,type:.info,s)
}
Then what do I need to do to see the logged messages in Console.app? By default, only log messages of type .error seem to show up.
I am wondering how to do this if I am running code on a device, not in the simulator.
Related:
Xcode 8 - os_log_debug and os_log_info logs are not displayed on new Mac console (unified logging)
Hilariously, the answer is that you just go to the Console.app's menu bar and select:
Action / Include Info Messages
Action / Include Debug Messages
Xcode 10.0 beta 6 (likely others too) won’t show debug messages logged from the simulator even after enabling Include Info Messages, Include Debug Messages in the Console.app. AFAIK there is no fix for this.
To see debug logs sent from the simulator you have to stream from a terminal instead:
xcrun simctl spawn booted log stream --debug --predicate 'subsystem == "es.com.jano.Myapp"'

IOS App crashes, but there is no crash log available

I am starting my App from XCode and with a specific behaviour it just disconnects and the app disappears from my IPAD screens.
So I guess the app crashes.
Now my question. Where Do I get this crashlog ? I already looked into
Window -> Devices -> choose my Ipad -> "View Device Logs" . But I cant see any actual crash log. I have some from earlier times, but my actual doesn't show up.
How do I get a crashlog ?
check if you have enabled Share with app developers option in settings. Privacy -> Analytics -> Share with App Developers.Xcode was not showing crash logs, enabling this helped me
You need to look at the device console under windows, devices in Xcode.
Its possibly saying something about code signing, bundle id's or missing libraries.
Try to delete the crash logs from "earlier times".
You have maximum amount of saved crash logs, and if you passed it - you won't see the new crash logs

Resources