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

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).


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.

Tags:

Logging

Ios

Macos