ios logger
开始吧 (Let’s Start)
We will go through in short with migration from existing print
logging to the new Apple Logging API.
我们将简要介绍从现有的print
日志到新的Apple Logging API的迁移。
Then we will revisit the OSLog
API and check differences between existing old OSLog
and new iOS 14 Logger
APIs.
然后,我们将重新访问OSLog
API,并检查现有的旧OSLog
和新的iOS 14 Logger
API之间的区别。
To start off, let’s convert a simple print statement from this, which we use often in our project to log messages:
首先,让我们从中转换一个简单的打印语句,我们在项目中经常使用它来记录消息:
print(“Start network call”)
to this:
对此:
let log = OSLog.init(subsystem: "com.mandy.loggingdemo", category: "main")
os_log("Start network call", log: log)
Remember to use os_log
API for this; you have to write import os.log
statement. The unified logging system has been Apple’s standard for logging since iOS 10. (It was announced at WWDC 2016.)
请记住为此使用os_log
API; 您必须编写import os.log
语句。 自iOS 10以来,统一日志记录系统一直是Apple的日志记录标准(已在WWDC 2016上宣布 )。
You can consider subsystem as a unique bundle id of your app and category as app modules.
您可以将子系统视为应用程序的唯一捆绑包ID,并将类别视为应用程序模块。
Now, in iOS 14, Apple has introduced a new Logger
API. To use this API, we have to write same import os.log
statement, but the syntax is quite different to os_log
API.
现在,在iOS 14中,Apple引入了新的Logger
API。 要使用此API,我们必须编写相同的import os.log
语句,但是语法与os_log
API完全不同。
let logger = Logger.init(subsystem: "com.mandy.loggingdemo", category: "main")
logger.debug("Start network call")
You can view the os_log
and Logger
logs in Xcode console in debug mode and also in the Mac Console app.
您可以在Xcode控制台中以调试模式以及在Mac Console应用程序中查看os_log
和Logger
日志。
The Console app is very powerful; I would recommend using it as you can filter the values using subsystem and category and debug your app more extensively.
控制台应用程序非常强大; 我建议您使用它,因为您可以使用子系统和类别过滤值,并更广泛地调试应用程序。
It also helps when you want to debug issues of device