iPhone 终端将在屏幕上显示日志输出暂停,可以生成日志文件共享和调试信息,当真实设备没有连接到 Xcode 时。您可以共享、过滤日志等操作。使用 SQLite 存储日志信息,支持系统共享和屏幕 FPS 显示
预览 GIF 图片 | Xcode 调试 GIF |
---|---|
![]() |
![]() |
预览 | 分享 & FPS |
---|---|
![]() Xcode 预览 ![]() |
![]() |
您可以选择使用 CocoaPod 安装,或者直接将源文件下载到项目中。
pod 'DDLoggerSwift'
您可以将位于 pod
文件夹中的文件拖放到项目中的项目下。
Import DDLoggerSwift
DDLoggerSwift.show()
三种输出方法的字体颜色不同,对应的 printLog 类型也不同。
printDebug(log) //the log will not be written to the window, only output in xcode
printLog(log) // Log's textColor is green
printWarn(log) // log's textColor is yellow
printError(log) // Log's textColor is red
printPrivacy(log) // Output of encrypted data, the specific encryption method is described in the following encryption
输出格式
2021-08-11 10:07:28.378 ---- ⚠️⚠️ ---- File: ViewController.swift -- Line: 82 -- Function:ViewController.swift.onClickButton() ----
警告提示
2021-08-11 10:07:28.380 ---- ❌❌ ---- File: ViewController.swift -- Line: 84 -- Function:ViewController.swift.onClickButton() ----
错误出现
2021-08-11 10:07:28.381 ---- ⛔️⛔️ ---- File: ViewController.swift -- Line: 86 -- Function:ViewController.swift.onClickButton() ----
AAuKjIm5hC2jiPqz7OKHAngWspeACyWZufDguqdOcugituhWV8jnbr/6SHYoK0/9
2021-08-11 10:07:28.383 ---- ✅✅ ---- File: ViewController.swift -- Line: 89 -- Function:ViewController.swift.onClickButton() ----
{
"77777" : "数据库的复健科花见花开会尽快圣诞节开发和金黄色的费四大皆空回复就开始和豆腐是砍价的回复斯柯达金凤凰",
"hhhhhhh" : "撒旦法是打发斯蒂芬是打发斯蒂芬"
}
2021-08-11 10:07:28.388 ---- 💜💜 ---- File: ViewController.swift -- Line: 76 -- Function:ViewController.swift.onClickButton() ----
测试输出,默认不会写入数据库
DDLoggerSwift.isFullLogOut = true
如果设置为 true
,则输出格式如下,包括输出文件、调用行数和函数名
2021-08-11 10:07:28.378 ---- ⚠️⚠️ ---- File: ViewController.swift -- Line: 82 -- Function:ViewController.swift.onClickButton() ----
警告提示
2021-08-11 10:07:28.380 ---- ❌❌ ---- File: ViewController.swift -- Line: 84 -- Function:ViewController.swift.onClickButton() ----
错误出现
2021-08-11 10:07:28.381 ---- ⛔️⛔️ ---- File: ViewController.swift -- Line: 86 -- Function:ViewController.swift.onClickButton() ----
AAuKjIm5hC2jiPqz7OKHAngWspeACyWZufDguqdOcugituhWV8jnbr/6SHYoK0/9
2021-08-11 10:07:28.383 ---- ✅✅ ---- File: ViewController.swift -- Line: 89 -- Function:ViewController.swift.onClickButton() ----
{
"77777" : "数据库的复健科花见花开会尽快圣诞节开发和金黄色的费四大皆空回复就开始和豆腐是砍价的回复斯柯达金凤凰",
"hhhhhhh" : "撒旦法是打发斯蒂芬是打发斯蒂芬"
}
2021-08-11 10:07:28.388 ---- 💜💜 ---- File: ViewController.swift -- Line: 76 -- Function:ViewController.swift.onClickButton() ----
测试输出,默认不会写入数据库
如果设置为 false
,则输出格式如下
2021-08-11 10:10:33.309 ---- ⚠️⚠️ ----
警告提示
2021-08-11 10:10:33.310 ---- ❌❌ ----
错误出现
2021-08-11 10:10:33.312 ---- ⛔️⛔️ ----
AAuKjIm5hC2jiPqz7OKHAngWspeACyWZufDguqdOcugituhWV8jnbr/6SHYoK0/9
2021-08-11 10:10:33.318 ---- ✅✅ ----
{
"hhhhhhh" : "撒旦法是打发斯蒂芬是打发斯蒂芬",
"77777" : "数据库的复健科花见花开会尽快圣诞节开发和金黄色的费四大皆空回复就开始和豆腐是砍价的回复斯柯达金凤凰"
}
2021-08-11 10:10:33.323 ---- 💜💜 ----
测试输出,默认不会写入数据库
DDLoggerSwift.isSyncConsole = true
DDLoggerSwift.userID = "1001"
DDLoggerSwift.cleanLog()
DDLoggerSwift.close()
DDLoggerSwift.hide()
DDLoggerSwift.maxDisplayCount = 100
DDLoggerSwift.deleteLogFile()
DDLoggerSwift.logExpiryDay = 30
将包含的日志级别存储在数据库中。默认情况下,调试级别不存储
DDLoggerSwift.storageLevels = [.info, .warn, .error, .privacy]
//today
DDLoggerSwift.getAllLog()
//Special date
DDLoggerSwift.getAllLog(date: Date(timeIntervalSinceNow: 1000))
如果您想获取所有日志文件,您可以获取存储日志的文件夹,返回 URL 结果,然后自行遍历处理
DDLoggerSwift.getDBFolder()
例如
let dbFolder = DDLoggerSwift.getDBFolder()
if let enumer = FileManager.default.enumerator(atPath: dbFolder.path) {
while let file = enumer.nextObject() {
if let file: String = file as? String {
if file.hasSuffix(".db") {
//Get the specific log file log
let logFilePath = dbFolder.appendingPathComponent(file, isDirectory: false)
}
}
}
}
如果您不希望用户看到日志输出窗口,但只想让他们分享日志,您可以调用
DDLoggerSwift.showShare()
如果您希望用户上传 DB 文件,除了自行遍历外,我们还提供快捷方案。如分享,调用
DDLoggerSwift.showUpload()
上传选项将出现。用户选择后的回调在 uploadcomplete
中确定。您可以实现回调,例如
DDLoggerSwift.uploadComplete = { file in
print(file)
//Process upload
}
在短时间内加载大量输出内容可能会导致大量 CPU 运算。因此,可以采用节流方法,并设置一个按秒计算的定时刷新界面,默认值为 0
DDLoggerSwift.throttleTime = 2
如果您想自定义输出内容,可以集成并使用这种类型的 LogContent
协议。例如,您可以打印 URL
类型,仅输出其 路径
。您可以直接设置返回的 logStringValue
。
extension URL: LogContent {
public var logStringValue: String {
return self.path
}
}
如果您不想在调试时让用户看到敏感信息,您可以通过以下两步设置加密:
// 1. Set the encryption password, 32 characters
DDLoggerSwift.privacyLogPassword = "12345678901234561234567890123456"
// 2, output encrypted content
printPrivacy("This is test data 222 for encrypted data")
设置后,显示窗口中的内容为 此内容已加密,请解密后查看
,输入设置的加密密码,单击解密以显示加密信息内容。
- 如果在显示窗口中已解密数据,则此时共享文件的内容不会加密,所有内容将显示出来。
- 如果不在显示窗口中解密,则此时共享的文件内容是AES加密内容,您可以在
AES 在线解密网站
搜索解密内容,设置如下:
- 模式: CBC
- 填充: Pkcs7
- 数据块: 128 位
- 偏移:
abcdefghijklmnop
- 编码: Base64
- 字符集: UTF8
- 密码: 您在SDK中设置的密码
然后单击解密。
以下是一些推荐的网络网站,您也可以自行通过 Google 搜索:
此库使用 SQLite
来存储日志信息。因此,您可以使用 DDLoggerSwift_Mac,它是 DDLoggerSwift
的客户端,可协同查看导出的SQLite数据。
从 3.0.0
版本开始,您可以使用上述日志查看工具来实现本地网络上的实时日志查看,并使用简单的配置界面。
- 增加本地网络功能
pod 'DDLoggerSwift/socket'
- 在项目的
info.plist
中添加本地网络描述和Bonjour
的服务字段。
<key>NSBonjourServices</key>
<array>
<string>_DDLoggerSwift._tcp</string>
</array>
<key>NSLocalNetworkUsageDescription</key>
<string>Find the local network to use the Bonjour feature</string>
**注意**: NSBonjourServices
的类型值应与 DDLoggerSwift.socketType
保持一致。在 DDLoggerSwift
代码中的 socketType
可以进行定制。修改后,info.plist
应相应地进行修改。
无需其他配置,您可以使用 DDLoggerSwift_Mac 在同一本地网络下查看设备日志。
- 为了便于查看,它分为三种类型:info、warning 和 error。它们对应三种不同的颜色,以便于查看。
- 单击相应的单元格,可以直接将输出日志复制到系统剪贴板。
- 共享被调用的系统共享。您能共享哪种软件取决于您的手机上安装了哪种软件。
- 共享的日志文件可以在任何文本编辑器中查看。当在 'vscode' 中查看时,代码将被突出显示。
本项目基于 MIT 许可证。