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

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"'

Related

How to log in Objective C when iPhone is not attached to Xcode

I am using Xcode Version 13.4.1 (13F100) on a MacBook Pro with macOS Monterey 12.4 and programming in Objective-C.
I would like to log debug messages when my app is running on the iPhone device AND is NOT attached to Xcode. I tried OSLog like described here: https://www.avanderlee.com/debugging/oslog-unified-logging/
However when I open the Console App and select my connected iPhone after running the app the log is empty and I have to explicitly click on "Start streaming" only then I can see my log when I start the app again.
I would like to log when the iPhone is not connected and the app is running and then close the app and connect the iPhone and see the log from the app run. I don't want live streaming but look into past logs.
Is there a way to do that?
I wrote to Apple Developer Support and they said the following:
If you log in your code using OSLog, then you can retrieve those
messages using the Terminal command ‘log’ later on.
For example, with the device named “iPhone" connected to the Mac, you
can retrieve the log info from the last 3 minutes with:
> % sudo log collect --device-name iPhone --last 3m
You can then use Console to look through the saved logs, or you can
parse out the log data from the command line with the ‘log' command.
See its man page for usage.
Here a link on how to use OSLog:
https://www.avanderlee.com/debugging/oslog-unified-logging/
In Objective-C this worked for me:
#import <os/log.h>
os_log(OS_LOG_DEFAULT, "Some message");
I followed Apple Developer Support's advice and it worked for me.

Swift, iOS 14: where to find device logs?

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/

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

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.

Info and Debug Unified Logging messages not appearing in console when running iOS simulator

I can't see Debug level or Info level log messages in the console app when running from the the iOS simulator.
I can see Default, Error and Fault levels in the console with the simulator.
I can see all Debug, Info, Default, Error and Fault levels in the console with a real non-simulated device.
I'm using the same console app filter settings with the simulator and in a real device.
I'm using XCode 8.3.3 with a simulator running iOS 10.3.
I'm using the Unified Logging System in my iOS app, as described here:
https://developer.apple.com/documentation/os/logging
https://developer.apple.com/videos/play/wwdc2016/721/
How can I get these Debug level and Info level log messages to display when running against the simulator?
In the Console app's menu:
Action ->Include Info Messages and/or ->Include Debug Messages
Update:
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.mydomain.appname --mode level:debug
This turns on .debug-level logging for subsystem "com.mydomain.appname" (which includes .info and .default messages).
Separately, 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.mydomain.appname --mode level:debug,persist:debug
(subset of my answer at Viewing os_log messages in device console)
This seems to be the expected behaviour, unfortunately I can't point to any documentation to back this up, however the Info and Debug level log messages are sent to Xcode's console, not the Console.app.

Resources