xcode Apple 推荐的 Swift 日志记录方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50728431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:40:25  来源:igfitidea点击:

Apple Recommended Logging approach for Swift

iosswiftxcodeerror-loggingoslog

提问by user1046037

Note:

笔记:

This is not a duplicate of the linked questions

这不是链接问题的重复

Goal:

目标:

  • I am not looking for a printvs NSLogdifferences
  • 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.
  • 我不是找一个printVSNSLog差异
  • 事实上,我不想使用它们中的任何一个(目前正在使用print
  • 我正在寻找 Apple 推荐的方式,只是似乎找不到命令/文档,我只知道它存在。

Present implementation:

目前的实现:

Presently I am using printstatements with some global functions

目前我正在使用print带有一些全局函数的语句

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
  1. 处理错误的推荐方式/方法是什么(我不想使用 NSLog,因为它们会写入设备的控制台)
  2. 这仅用于开发期间的调试目的

回答by Honey

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

看看os_log。它提供了您正在寻找的所有东西。

Disclaimer:

免责声明:

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

我强烈建议您从 Swift 论坛中查看此主题。tl;博士

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

尽管这是 Apple 的推荐,但由于对检索日志的担忧,它的使用仍存在争议:

  • 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.
  • 检索日志不是一个简单的过程。其实很难。看这里
  • 对于大多数用户,日志文件可以是 100-300 Mbs。这使得发送变得困难。

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 answerby Rob. He discusses that NSLogis deprecated and some of the benefits of using the new os_loglibrary.
  • You can also get the logs from using the approach mentioned here. Make sure you see ?answers.

The reason os_logis so powerful is because:

之所以os_log如此强大,是因为:

  • offers different log levels
  • has different categories
  • privateand publiclogs
  • it's lightweight and built by Apple. Doesn't require pods
  • unlike printwhich is only available during debugging, os_logcan be used to peek into a released app (in realtime) and view the logs in the console app.
  • 提供不同的日志级别
  • 有不同的类别
  • privatepublic日志
  • 它是轻量级的,由 Apple 制造。不需要豆荚
  • print仅在调试期间可用的不同,os_log可用于查看已发布的应用程序(实时)并在控制台应用程序中查看日志。

enter image description here

在此处输入图片说明

This is great for observing application life cycle changes free of the greedy Xcode. Xcode will not allow the app to be put in a suspended state...

这对于在没有贪婪的 Xcode 的情况下观察应用程序生命周期变化非常有用。Xcode 不允许将应用程序置于挂起状态...



Note:os_logis only available to +iOS10

注意:os_log仅适用于+iOS10

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

还有来自 WWDC 2018 和 2019 的新视频,但更关注os_signpost. 看: