Apple推荐的Swift记录方法 [英] Apple Recommended Logging approach for Swift

查看:96
本文介绍了Apple推荐的Swift记录方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:

这不是链接问题的重复

目标:

  • 我不是要寻找 print NSLog 的区别
  • 实际上我不想使用它们中的任何一个(目前使用 print )
  • 我正在寻找Apple推荐的方法,只是似乎找不到命令/文档,我只知道它存在.

当前实现:

目前,我正在将 print 语句与某些全局函数一起使用

问题

  1. 处理错误的推荐方法/方法是什么(我不想使用NSLog,因为它们会写入设备控制台)
  2. 这仅用于开发过程中的调试目的

解决方案

看看

这非常适合观察应用程序生命周期的变化,而无需贪婪的Xcode ,即如果您在连接Xcode时进行调试,则Xcode不允许将应用置于挂起状态...


注意: os_log 仅适用于+ iOS10

还有来自WWDC 2018和2019的新视频,但重点放在 os_signpost 上.参见:

Note:

This is not a duplicate of the linked questions

Goal:

  • I am not looking for a print vs NSLog differences
  • In fact I don't want to use either of them (presently using print)
  • I am looking for an Apple recommended way, just can't seem to find the command / documentation, I just know it exists.

Present implementation:

Presently I am using print statements with some global functions

Question

  1. What is the recommended way / approach to handle errors (I don't want to use NSLog as they would write into the device's console)
  2. This is only for debugging purposes during development

解决方案

Take a look at os_log. It offers all the things you're looking for.

Disclaimer:

I highly recommend you see this thread from Swift forums. tl;dr

Even though it's' Apple's recommendation, its usage is debated due to concerns about retrieving logs:

  • retrieving logs is not a trivial process. It's actually difficult. See here
  • For most users the log file can be 100-300 Mbs. Which makes it hard to send.

It's great for debugging during development, but laborious to trigger, retrieve, send by your app users.


Example:

let customLog = OSLog(subsystem: "com.your_company.your_subsystem_name", category: "Category")
os_log("This is info that may be helpful during development or debugging.", log: customLog, type: .debug)

Some great references:

  • WWDC 2016 Unified Logging and Tracing.
  • This answer by Rob. He discusses that NSLog is deprecated and some of the benefits of using the new os_log library.
  • You can also get the logs from using the approach mentioned here. Make sure you see ? answers.

The reason os_log is so powerful is because:

  • offers different log levels
  • has different categories
  • private and public logs
  • it's lightweight and built by Apple. Doesn't require pods
  • unlike print which is only available during debugging, os_log can be used to peek into a released app (in realtime) and view the logs in the console app.

This is great for observing application life cycle changes free of the greedy Xcode i.e. if you’re debugging while connected to Xcode , then Xcode doesn’t allow the app to be put in a suspended state...


Note: os_log is only available to +iOS10

There are new videos as well from WWDC 2018 and 2019, but have a higher focus on os_signpost. See:

这篇关于Apple推荐的Swift记录方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆