43

From background of Objective C when I use NSLog() it prefixes the text with the date time stamp, but when I use print() on Swift it only prints the text

So it there is a way to make it print the time stamp as well, or am I doing some thing wrong?

Mahmoud Adam
  • 5,422
  • 5
  • 35
  • 61
  • I'm not particularly sure, but I'm pretty sure this has to do with the fact that `NSLog` prints to the Console (and your log is visible in Console.app) but `print` in Swift doesn't do that. – Arc676 Oct 27 '15 at 09:49
  • I have no evidence to support this 2nd point, but I believe that Xcode actually takes information from the Console and prints everything in a certain category even if it's not related to the program you're running. I've seen some irrelevant console messages in my Xcode. Maybe Swift's `print` just prints to the Xcode console? – Arc676 Oct 27 '15 at 09:51
  • 4
    check this question: http://stackoverflow.com/questions/25951195/swift-print-vs-println-vs-nslog – Kubba Oct 27 '15 at 09:51

6 Answers6

57

Because print is not NSLog. It is as simple as that.

NSLog is a logging tool in Foundation that writes to the Apple System Log facility which appears on the console.

print(…) is a print function in the Swift Standard Library that writes to standard out, which appears on the console in debug sessions.

You could add Date() to your print parameter to print the current time and date. (Or Date().description(with: Locale.current) to get it in your local time zone.)

Or you could just use NSLog which is available in Swift too (if you import Foundation).

shim
  • 7,170
  • 10
  • 62
  • 95
Hermann Klecker
  • 13,792
  • 4
  • 45
  • 69
26

Swift:

NSLog("this will print with dates")
Juan Boero
  • 5,451
  • 37
  • 56
9

This one just outputs a simple timestamp, but can be easily modified to include additional text if you want.

Additionally, it relies on a lazy DateFormatter to avoid expensive initializations.

import Foundation

class Timestamp {
    lazy var dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS "
        return formatter
    }()

    func printTimestamp() {
        print(dateFormatter.string(from: Date()))
    }
}

let timestamp = Timestamp()
timestamp.printTimestamp() // 2018-07-05 12:57:08.725
timestamp.printTimestamp() // 2018-07-05 12:57:08.727 (uses the same formatter)
CodeBender
  • 30,010
  • 12
  • 103
  • 113
6

Here is a suggested function you can use instead of print()

func printLog(log: AnyObject?) {
    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS "
    print(formatter.stringFromDate(NSDate()), terminator: "")
    if log == nil {
        print("nil")
    }
    else {
        print(log!)
    }
}
david72
  • 6,327
  • 2
  • 31
  • 54
  • 15
    Creating an instance of NSDateFormatter is costly. So, ideally, the `formatter` would have to be created only once, outside your method, to avoid calling NSDateFormatter() each time the method is called. – Eric Aya Jul 05 '16 at 21:50
  • It is not about what you want - in this case, this should be done. – Jakub Truhlář Mar 10 '17 at 18:18
1

Make your own class function for print, eg printWithDate() and prepend the date to the output. Then you can use this everywhere without needing to add the date each time you want to print.

totiDev
  • 4,759
  • 3
  • 27
  • 28
1

You can use the Logging os module to do that in Swift: https://developer.apple.com/documentation/os/logging

Specifically os_log: https://developer.apple.com/documentation/os/2320718-os_log

import os.log
os_log("Message %d", type: .info, value)
agirault
  • 1,430
  • 14
  • 22