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?

sobri
  • 1,626
  • 15
  • 28

2 Answers2

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
HuaTham
  • 7,486
  • 5
  • 31
  • 50
  • Interesting! I was also attempting to use it from Swift. Hadn't thought to check if it was acting differently for the Cs. – sobri Mar 13 '17 at 08:36
  • 3
    I filed bug report to Apple and they have fixed this for Swift. Should come out in the near future :-) – HuaTham Mar 13 '17 at 08:46
  • 9
    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. – bruce1337 Jun 23 '18 at 07:12
  • 1
    Still not working for me with Xcode 10.1. See https://stackoverflow.com/questions/52366951/apple-unified-logging-how-to-get-file-name-and-line-number/52376227#52376227 for an ugly way to do it manually - but I think I'll just wait and hope this gets fixed eventually. – Austin Oct 30 '18 at 23:06
0

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")
chetan anand
  • 103
  • 1
  • 7