How can I open/view iOS OSLogs stored on device?
You did not specify iOS, macOS, or tvOS in your question, so I am keeping my answer generic across all platforms.
Review WWDC 2016 - Session 721 - Unified Logging and Activity Tracing.
Logging data is stored in a new compressed binary format (.tracev3
or .logarchive
files), that's what those "numbers" are. You must use Console.app or the log
command line tool to open these files.
You're looking in the wrong place for the log files. From the session video:
Those files are now stored under /var/db/diagnostics with additional supporting files in /var/db/uuidtext. There are new tools to access that data, there's a new Console, a new log command line tool and one of the things you have to keep in mind is because the data is now stored in a binary format you must use the new tools to access it. So you can no longer grep through logs you have to use our tools to do the surfing through it.
There's also another new type of file the .logarchive, which is there for portability of log data. Essentially a .logarchive is a collection of information out of /var/db/diagnostics and you uuidtext collected together into a single file that's easier to transfer to email, to attach to bug reports and the like.
If you redirect stdout or stderr to a file (such as the code in this example), it should be in the Documents directory of your application's sandbox (or whatever directory you specified). Logs written to /var/db/diagnostics are generally unavailable to end users on iOS and tvOS.
Viewing os_log messages in 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.
Logging data on device and retrieving the log
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
Just add this block of code in application:didFinishLaunchingWithOptions
method in the app delegate file and it will create a log file in app document directory on iPhone which logs all console log events. You need to import this file from iTunes to see all console events.
Note: In the .plist file make sure that Application supports iTunes file sharing
is exists and is set to YES
so that you can access through iTunes.
To get Logfiles :
Launch itunes, after your device has connected select Apps - select your App - in Augument Document you will get your file. You can then save it to your disk
How can I retrieve messages logged with os_log from iPad/iPhone?
On iOS 11, you can now capture a sysdiagnose and get access to the sysdiagnose for emailing (don't email this, it's huge), or exporting to dropbox/airdrop to your Mac. Instructions.
The gist of it is :
- hold down volume up and volumn down and power button for 1.5 seconds until the screen vibrates.
- then wait for upto 10 minutes and on your iOS 11 device go to:
Settings > Privacy > Analytics > Analytics Data > sysdiagnose_[xxx] > tap share icon in the nav bar
. For me it took less than a minute.
Search the logs for sysdiagnose
. It will be something like sysdiagnose_2018.12.13_14-16-19-0500_iPhone_OS_iPhone_15G77
Then share it through AirDrop. You don't need a cable and iTunes anymore.
Apple Recommended Logging approach for Swift
Take a look at os_log
. It offers all the things you're looking for.
NEW STUFF
Also the more easier to use Logger API available since iOS14. See here as well
Since iOS15 you can also retrieve the logs using OSLogStore
. See here.
Also see Acquiring Crash Reports and Diagnostic Logs
Disclaimer:
I highly recommend you see this thread from Swift forums. tl;dr
Even though it's' Apple's recommendation, its usage is debated due to concerns about retrieving logs:
- retrieving logs is not a trivial process. It's actually difficult. See here
- For most users the log file can be 100-300 Mbs. Which makes it hard to send.
It's great for debugging during development, but laborious to trigger, retrieve, send by your app users.
Example:
let customLog = OSLog(subsystem: "com.your_company.your_subsystem_name", category: "Category")
os_log("This is info that may be helpful during development or debugging.", log: customLog, type: .debug)
Some great references:
- WWDC 2016 Unified Logging and Tracing.
- This answer by Rob. He discusses that
NSLog
is deprecated and some of the benefits of using the newos_log
library. - You can also get the logs from using the approach mentioned here. Make sure you see ? answers.
The reason os_log
is so powerful is because:
- offers different log levels
- has different categories
private
andpublic
logs- it's lightweight and built by Apple. Doesn't require pods
- unlike
print
which is only available during debugging,os_log
can be used to peek into a released app (in realtime) and view the logs in the console app.
This is great for observing application life cycle changes free of the greedy Xcode i.e. if you’re debugging while connected to Xcode , then Xcode doesn’t allow the app to be put in a suspended state...
Note: os_log
is only available to +iOS10
There are new videos as well from WWDC 2018 and 2019, but have a higher focus on os_signpost
. See:
- WWDC 2018 - Measuring Performance Using Logging
- WWDC 2019 - Getting Started with Instruments
- WWDC 2019 - Developing a Great Profiling Experience
Related Topics
How to Apply Borders and Corner Radius to Uibarbuttonitem
How to Install the Alamofire 4.0 in Xcode 8.0
How to Convert Any to Int in Swift
How to Use Swift Package Manager with an Existing MACos Project
Type Ccc Doesnt Conform to Protocol 'Nsobjectprotocol'
Nsnumberformatter:Show 'K' Instead of ',000' in Large Numbers
Swift Why Strcmp of Backspace Returns -92
Swift Package Manager - Type 'Bundle' Has No Member "Module" Error
Checking When a Date Has Passed - Swift
How to Read and Write Data to a Text File in Swift Using Playground
Leading Zeros for Float in Swift
Swift Bindings Won't Work Xcode 6 Beta 5
How to Update Swift from 3.1 to 3.2, But Not to 4.0
Cast to a Metatype Type in Swift
How Does Anyobject Conform to Nsobjectprotocol