ios logger_iOS 14的新Logger API与OSLog

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_logLogger日志。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值