Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple unified logging - how to get file name and line number

Tags:

logging

macos

ios

Apple's Unified Logging documentation states:

Don’t include symbolication information or source file line numbers in messages. The system automatically captures this information.

But in Console I don't see a filename, class name, function name, or line numbers.

How can I have those in my logs?

like image 635
gutte Avatar asked Sep 17 '18 11:09

gutte


People also ask

What is the best way to log files on a Mac?

OSLog as a replacement of print and NSLog is the recommended way of logging by Apple. It’s a bit harder to write, but it comes with some nice advantages compared to it’s better-known friends.

What is the unified logging system?

The unified logging system provides a comprehensive and performant API to capture telemetry across all levels of the system. This system centralizes the storage of log data in memory and on disk, rather than writing that data to a text-based log file. You view log messages using the Console app, log command-line tool, or Xcode debug console.

How do I log the username of my Device?

The username is logged as <private> instead which prevents your data from being readable by anyone inside the logs. Using the Console.app in combination with OSLog is recommended to get the most out of this way of logging. Start by selecting your device on the left in the devices menu.

How do I view log messages in Xcode?

This system centralizes the storage of log data in memory and on disk, rather than writing that data to a text-based log file. You view log messages using the Console app, log command-line tool, or Xcode debug console.


2 Answers

Indeed, even macOS Catalina beta (build 19A501i) does not show source lines in Console.app.

However, the log command line tool does show source information for simulated devices (macOS 10.14 onwards).

The following command will show log information for all simulated devices currently booted (running) in the simulator. If no simulated devices are currently running the command will fail.

xcrun simctl spawn booted log stream --level debug --color always --source

You can filter out everything which does not come from your program by using a subsystem in os_log calls, and applying a predicate to the streamed logged data. For example, if your subsystem is com.subsystem.my, you can use

xcrun simctl spawn booted log stream --level debug --color always --predicate '(subsystem BEGINSWITH "com.subsystem.my")' --source

The source code information will be shown after the TTL column.

You can also filter by process (i.e. the name of your target), in case you didn't set up a subsystem

xcrun simctl spawn booted log stream --level debug --color always --predicate '(process == "MyProcess")' --source

However, this will normally result in too many log messages since information logged by other frameworks will also be included.

Also, instead of booted, you can use the name of the actual simulated device (to stream log data from that device alone).

like image 133
Marcpek Avatar answered Oct 11 '22 16:10

Marcpek


os_log doesn't currently give line numbers/function names for Swift code in Console.app, or through the log stream command.

If you really need it - you can use Literal Expressions to pass the information manually via NSLog or os_log like this:

os_log(.info, "Log message from file: %s, line: %i, column: %i", #file, #line, #column)

It may be tempting to wrap os_log to always include this information, but Apple suggests not doing so for performance reasons.

like image 31
lobstah Avatar answered Oct 11 '22 16:10

lobstah