Swift: print() vs println() vs NSLog()

https://stackoverflow.com/questions/25951195/swift-print-vs-println-vs-nslog#

  • print function prints message in the xcode console when debugging apps
  • printlnis a variation of this that was removed in Swift2 and is not used any more

NSLog

  • NSLogadds a timestamp and identifier to the output; whereas print will not
  • NSLog statements appear in both the device's console and debugger's console ; print only appears in the debugger's console
  • NSLog in iOS 10-13/macOS 10.12-10.x uses printf-style format strings
NSLog("%0.4f", CGFloat.pi)
  • NSLog from iOS 14/maxOS 11 can use string interpolation

Logger - iOS14/macOS 11

  • introduction https://developer.apple.com/videos/play/wwdc2020/10168/
  • import os
  • output message to both the Xcode debugging console and the device console
  • Create a Logger and log a message to it
    • observe the app via the external Console app, can filter on the basis of the subsystem and category
  • specify different types of logging messages
    • .info
    • .debug
    • .error
    • .fault
    • .critical
    • .notice
    • .trade
  • for url
os_log("url=\(url, privacy: .public)")

os_log - iOS 10/macOS 10.12

https://www.avanderlee.com/workflow/oslog-unified-logging/

  • OSLog as a replacement of print and NSLog is the recommended way of logging by Apple
  • Using the Console app in combination with your logs can help you debug issues in a more efficient way
  • two of the advantages of using OSLong
    • has a low-performance overhead
    • is archived on the device for later retrieval
  • OSLog makes it possible to log per category

Log Levels

  • OSLogType
    • default(notice)
      • should be avoid
    • info
      • capture information that may be helpful
      • not essential for trobleshooting
    • debug
      • be intended for use in a development environment while actively debugging
    • error
      • be intended for reporting critical errors and failures
    • fault
      • be intended for capturing system-level or multi-process errors only
      • indicate a serious problem

Logging parameters

  • public or private
    if private, the content is logged as instead which prevents your data from being readable by anyone inside the logs (external Console app)

Improved APIs in iOS 14 and up

  • Using a Logger instance
extension Logger {
    private static var subsystem = Bundle.main.bundleIdentifier!

    /// Logs the view cycles like viewDidLoad.
    static let viewCycle = Logger(subsystem: subsystem, category: "viewcycle")
}

Logger.viewCycle.info("View did load!")
  • Removed restriction of static strings

Logger.viewCycle.debug("User \(username) logged in")

  • Setting the right privacy level
Logger.viewCycle.debug("User \(username, privacy: .private) logged in")
  • Log format - New alignment APIs
func log(_ person: Person) {
    Logger.statistics.debug("\(person.index) \(person.name, align: .left(columns: Person.maxNameLength)) \(person.identifier) \(person.age, format: .fixed(precision: 2))")
}

/// [statistics] 95 Antoine    F205DD9C-C92A-4B48-B27A-CF19C6081EB3 85.33
/// [statistics] 84 Jaap       C55C3F42-5C02-43E0-B416-2E0B7356A964 88.70
/// [statistics] 58 Lady       FD25FB54-51CA-4D6D-805E-547D29D5AE34 38.30
/// [statistics] 69 Maaike     4FDE8D73-ECBF-4015-AE5F-2AED7295D6B2 9.72
/// [statistics] 86 Jacobien   E200351B-920F-4351-9752-212912B42ECB 69.23

Architecting a logging service for iOS Apps

https://andreaslydemann.com/architecting-a-logging-service-for-ios-apps/

  • demonstrate local logging to an external file
  • 本文介绍了应用中本地日志服务搭建,并将日志写到文件中。
    • 打算直接使用Swift自带的日志记录系统,不再参考

Migrating to Unified Logging: Console and Instruments

https://www.raywenderlich.com/605079-migrating-to-unified-logging-console-and-instruments

  • this article is for os_log ,not for Logger,skip