How to find source file and line number from os_log()
Asked Answered
A

2

8

The Logging Apple reference for the new logging system in iOS 10 and macOS Sierra explicitly say not to include line number and source file info, because it is automatically captured.

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

But I have yet to be able to find any way of viewing these supposedly captured values. In the Console app I can see subsystem, category, process ID, etc, but nothing about file and line.

And the command line tool similarly appears to lack any options for displaying this information (unless I'm missing something).

Anyone figured it out yet?

Audun answered 23/11, 2016 at 6:30 Comment(0)
A
6

I don't think it's available in Swift yet, although you can see file and line number in C / C++ in Terminal. See Apple forum here.

I tried something similar to what's inside the forum by creating a simple command-line tool Xcode project:

import Foundation
import os.log

os_log("rrrr")

and type the following in Terminal: log stream --source --predicate 'eventMessage contains "rrrr"', and I got this:

Timestamp                       Thread     Type        Activity             PID    
2017-02-18 17:58:46.012381+0700 0x5067d    Default     0x0                  5637   <testLogSwift`_swift_os_log> rrrr

in contrast to what I got in C/C++ version, which shows the file and line number:

Timestamp                       Thread     Type        Activity             PID    
2017-02-18 17:55:05.056103+0700 0x4aa01    Default     0x0                  5218   <testLogging`main (main.cpp:13)> qqq
Allhallowtide answered 18/2, 2017 at 11:4 Comment(4)
Interesting! I was also attempting to use it from Swift. Hadn't thought to check if it was acting differently for the Cs.Audun
I filed bug report to Apple and they have fixed this for Swift. Should come out in the near future :-)Allhallowtide
Was this ever fixed? I am switching over to os_log for my iOS 11 app (written in Swift 4) with Xcode 9.4.1, and my os_log calls are not displaying line numbers or filenames when I view the logs in the Console app.Salverform
Still not working for me with Xcode 10.1. See #52367451 for an ugly way to do it manually - but I think I'll just wait and hope this gets fixed eventually.Ligature
P
-1

Till apple fixes this issue, I've created a simple extension

import os

extension Logger {
  init(subsystem: String = Bundle.main.bundleIdentifier ?? "", file: String = #file, function: String = #function, line: Int = #line, context: String) {
    let category = "\(file):\(line):\(function), \(context)"
    self.init(subsystem: subsystem, category: category )
  }
}

Usages:

Logger(context: "LoginFLow").debug("Hello World")

We can even remove param name to make it more cleaner:

import os

extension Logger {
  init(subsystem: String = Bundle.main.bundleIdentifier ?? "", file: String = #file, function: String = #function, line: Int = #line, _ context: String) {
    let category = "\(file):\(line):\(function), \(context)"
    self.init(subsystem: subsystem, category: category )
  }
}

Usages:

Logger("LoginFLow").debug("Hello World")

Note: If you wish to use the original Logger, just remove the context param and it will use its original init provided by apple.

Logger().debug("Hello World")
Paulettapaulette answered 11/1, 2023 at 2:57 Comment(1)
Creating a new Logger each time you want to log something is not a good solution!Terpstra

© 2022 - 2025 — McMap. All rights reserved.