printer-logger 1.4

printer-logger 1.4

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2017年12月
SwiftSwift 版本4.0
SPM支持 SPM

Hemang Shah 维护。



  • 作者:
  • hemangshah

您可以使用Printer打印以下类型的日志。

  • 成功
  • 错误
  • 🚧警告
  • 📣信息
  • 🚨警报

对于每种类型,它都会打印一个特定的表情符号和标题,这将帮助您轻松地识别日志的确切内容。此外,它看起来也很酷。

安装

1.手动 - 将Printer文件夹添加到您的项目。一切就绪。如果您只想添加PrinterViewController,请只添加Printer.swift

2.CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
target 'Sample' do
use_frameworks!
pod 'printer-logger', '~>1.3'
end

3.Carthage [即将推出]。 参考

您可以在特定的版本中阅读变更日志文件。

功能

  1. 不同的打印日志方式
  2. 纯文本日志
  3. 跟踪
  4. 所有可打印日志
  5. 所有可使用日志
  6. PrinterViewController
  7. 将日志保存到文件
  8. 刷新
  9. 自定义Printer
  10. 过滤日志
  11. 禁用日志
  12. 完成块
  13. 后台或前台日志
  14. 要将应用程序发布到AppStore吗?

附加信息

  1. 待办事项
  2. 致谢
  3. 谢谢
  4. 许可证

让我们看看您可以如何使用Printer。

Printer拥有一个单例,您应该始终使用它的单例。

Printer.log.show(id: "001", details: "This is a Success message.", logType: .success)

查看输出。不酷吗?

[✅ Success] [⌚04-27-2017 10:39:26] [🆔 101] ➞ ✹✹This is a Success message.✹✹

所以这里还有其他您可以用Printer做的选项。

Printer.log.show(id: "002", details: "This is a Error message.", logType: .error)
Printer.log.show(id: "003", details: "This is an Information message.", logType: .information)
Printer.log.show(id: "004", details: "This is a Warning message.", logType: .warning)    
Printer.log.show(id: "005", details: "This is an Alert message.", logType: .alert)

输出

[❌ Error] [⌚04-27-2017 10:41:39] [🆔 102] ➞ ✹✹This is a Error message.✹✹
[🚧 Warning] [⌚04-27-2017 10:41:39] [🆔 103] ➞ ✹✹This is a Warning message.✹✹
[📣 Information] [⌚04-27-2017 10:41:39] [🆔 104] ➞ ✹✹This is an Information message.✹✹
[🚨 Alert] [⌚04-27-2017 10:41:39] [🆔 105] ➞ ✹✹This is an Alert message.✹✹

不同的打印日志方式。

不想每次都指定日志类型?没问题,我们也有对应的函数。

Printer.log.success(id: "101", details: "This is a Success message. No need to specify logType.")
Printer.log.error(id: "102", details: "This is an Error message. No need to specify logType.")
Printer.log.warning(id: "103", details: "This is a Warning message. No need to specify logType.")
Printer.log.information(id: "104", details: "This is an Information message. No need to specify logType.")
Printer.log.alert(id: "105", details: "This is an Alert message. No need to specify logType.")

不想指定ID?我们也处理了这个问题。

Printer.log.success(details: "This is a Success message without ID.")
Printer.log.error(details: "This is an Error message without ID.")
Printer.log.warning(details: "This is a Warning message without ID.")
Printer.log.information(details: "This is an Information message without ID.")
Printer.log.alert(details: "This is an Alert message without ID.")

我们已重写了‘show’函数。

Printer.log.show(details: "This is a Success message.", logType: .success)
Printer.log.show(details: "This is an Alert message.", logType: .alert)

显示未来的日志。

Printer.log.showInFuture(id: "006", details: "This is a future Success message.", logType: .success, afterSeconds: 3)

这将会在指定秒后将打印日志。在这个例子中,成功日志将在三秒后打印。(3秒)

plainLog

不喜欢这种花哨的日志?别担心,我们还有一个简单的日志选项。

默认值: false 重要: 应该提前调用。

Printer.log.plainLog = true

例如plainLog 设置为 true 的情况。

[04-27-2017 10:50:30] ID ➞ 001 Details ➞ This is a Success message.
[04-27-2017 10:50:30] ID ➞ 002 Details ➞ This is a Error message.
[04-27-2017 10:50:30] ID ➞ 003 Details ➞ This is an Information message.
[04-27-2017 10:50:30] ID ➞ 004 Details ➞ This is a Warning message.
[04-27-2017 10:50:30] ID ➞ 005 Details ➞ This is an Alert message.

我们增加了一个 new.plain 类型并添加了 show() 函数。

Printer.log.show(id: "001", details: "This is a Plain message.", logType: .plain)

当你只想输出少量的普通日志时,这很有用。

重要: 你设置的任何属性都应该在打印日志之前设置,以获取正确的效果。

建议: 你总是可以在 AppDelegate.swift 文件中设置所有属性来自定义 Printer

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        //set the properties and call the specific function as per the need.

        //Printer.log.plainLog = true
        Printer.log.addLineAfterEachPrint = true
        Printer.log.capitalizeTitles = true
        Printer.log.capitalizeDetails = true
        Printer.log.printOnlyIfDebugMode = true
        
       //Applied filters to only print success and alert type logs.
       //Printer.log.filterLogs = [.success, .alert]
       
        Printer.log.onLogCompletion = { (log) in
          //print(log)
          //print(log.0)
        }

        //Printer.log.hideTitles()
        //Printer.log.hideEmojis()

        return true
    }

这将全局设置属性,并在整个应用程序生命期内可用。

跟踪

想打印文件名、函数名和行号吗?

重要: 应该在每次打印跟踪时调用。

Printer.log.trace()

Printer.Trace ➞ [05-02-2017 14:58:38] ViewController.swift ➞ viewDidLoad() #40

自动跟踪

默认值: true 重要: 在记录之前应将 keepAutoTracing 设置为 true

这将打印与调用 trace() 相同的跟踪信息。如果你不喜欢它,只需将 keepAutoTracing 设置为 false

打印所有日志

想打印特定用例的所有日志?

重要: 在记录之前应将 keepTracking 设置为 true。即使将 keepAutoTracing 设置为 false 并将 showTrace 设置为 true,你也会看到跟踪信息。这在你不希望在记录时跟踪时很有用。

Printer.log.all(showTrace: true)

[All Logs] [Success] [05-15-2017 14:28:03] Id:001 Details:This is a Success message.
[Trace] ➞ ViewController.swift ➞ viewDidLoad() #58
[All Logs] [Error] [05-15-2017 14:28:03] Id:002 Details:This is a Error message.
[Trace] ➞ ViewController.swift ➞ viewDidLoad() #59
[All Logs] [Information] [05-15-2017 14:28:03] Id:003 Details:This is an Information message.
[Trace] ➞ ViewController.swift ➞ viewDidLoad() #60
[All Logs] [Warning] [05-15-2017 14:28:03] Id:004 Details:This is a Warning message.
[Trace] ➞ ViewController.swift ➞ viewDidLoad() #61
[All Logs] [Alert] [05-15-2017 14:28:03] Id:005 Details:This is an Alert message.
[Trace] ➞ ViewController.swift ➞ viewDidLoad() #62

你还可以进行过滤。

Printer.log.all(filterLogTypes: [.alert], showTrace: true)

这只会打印带有跟踪信息的 .alert 类型跟踪日志。

[All Logs] [Alert] [05-15-2017 14:28:03] Id:005 Details:This is an Alert message.
[Trace] ➞ ViewController.swift ➞ viewDidLoad() #62

all() 函数总是打印普通日志。 没有花哨的东西

获取所有日志

想获取所有日志?

//let array = Printer.log.getAllLogs()
let array = Printer.log.getAllLogs(filterLogTypes: [.success])
if !array.isEmpty {
    array.forEach({ (log) in
         print(log.details)
        //or do something else.
    })
}

用例

  • 存储在某处。
  • 使用日志详细信息执行 API 调用。
  • 做 [Printer] 不支持的事情。

PrinterViewController

PrinterViewController 中查看所有打印机日志。您还可以在视图控制器内进行日志过滤。

重要: PrinterViewController 基于 Printer 的设置属性,并且工作方式完全相同,所以请确保已设置正确的属性。

用例

  • 要查看在测试 iDevice 或模拟器上的应用程序时在应用程序内部的全部日志。
  • 不需要检查 Xcode 或控制台。

功能

  • 根据类型筛选日志。
  • 复制特定的日志。
  • 易于设置。

即将推出

  • [ ] 通过电子邮件发送日志文件。
  • [ ] 搜索日志。
  • [ ] 在日志文件内设置属性。例如:普通日志 [开关] 就像这样!
  • [ ] 清除日志。
  • [ ] Air Print。
  • [ ] 查看所有日志文件。
  • [ ] 日志文件管理。
  • [ ] 将文本日志文件导出为 PDF。
All Logs No Logs Alert Logs

如何使用?

如果你喜欢手动安装。

你总是可以使用 Printer 而不一定需要 PrinterViewController。但建议添加此类以获得更好的日志记录。

  1. PrinterTableViewCell.swiftPrinterViewController.swiftPrinter.storyboardPrinter.swift 添加到您的项目中。你也可以简单地添加 Printer 文件夹。
  2. 所有内容都已添加,现在你只需要编写以下代码从你的应用程序中显示 PrinterViewController

始终将其添加到某个位置(例如:导航栏、侧边菜单、标签栏、应用设置),以便您可以在开发过程中始终展示它。

    let printerStoryboard = UIStoryboard.init(name: "Printer", bundle: Bundle.main)
    let navcontroller = UINavigationController.init(rootViewController: (printerStoryboard.instantiateViewController(withIdentifier: "PrinterViewControllerID")))
    self.present(navcontroller, animated: true, completion: nil)

将日志保存到文件

想要创建一个用于日志的文件?我们也提供了相应的解决方案。

let array = Printer.log.getAllLogs()
if !array.isEmpty {    
    Printer.log.saveLogToFile(logs: array)
}        

所有日志都将创建在打印文件夹下的单独文件中。

删除所有日志文件?

Printer.log.deleteLogFiles()

清空

想要删除所有日志文件并释放一些空间?

Printer.log.flush()

自定义打印机

您可以在每个日志后面添加一行。

默认值: false 重要: 应该提前调用。

Printer.log.addLineAfterEachPrint = true

当addLineAfterEachPrint设置为true的情况。

[✅ Success] [⌚04-27-2017 10:53:28] [🆔 001] ➞ ✹✹This is a Success message.✹✹
________________________________________________________________________________________
[❌ Error] [⌚04-27-2017 10:53:28] [🆔 002] ➞ ✹✹This is a Error message.✹✹
________________________________________________________________________________________
[📣 Information] [⌚04-27-2017 10:53:28] [🆔 003] ➞ ✹✹This is an Information message.✹✹
________________________________________________________________________________________
[🚧 Warning] [⌚04-27-2017 10:53:28] [🆔 004] ➞ ✹✹This is a Warning message.✹✹
________________________________________________________________________________________
[🚨 Alert] [⌚04-27-2017 10:53:28] [🆔 005] ➞ ✹✹This is an Alert message.✹✹
________________________________________________________________________________________

大写标题&详情

您甚至可以大写日志的标题和详情。

默认值: false 重要: 应该提前调用。

Printer.log.capitalizeTitles = true

默认值: false 重要: 应该提前调用。

Printer.log.capitalizeDetails = true

当capitalizeTitles和capitalizeDetails都设置为true的情况。

[✅ SUCCESS] [⌚04-27-2017 11:09:37] [🆔 001] ➞ ✹✹THIS IS A SUCCESS MESSAGE.✹✹

不想显示表情符号吗?

重要:应该在事先调用。

Printer.log.hideEmojis()

hideEmojis()被调用。

[Success] [04-27-2017 11:08:45] [001] ➞ ✹✹This is a Success message.✹✹
[Error] [04-27-2017 11:08:45] [002] ➞ ✹✹This is a Error message.✹✹
[Information] [04-27-2017 11:08:45] [003] ➞ ✹✹This is an Information message.✹✹
[Warning] [04-27-2017 11:08:45] [004] ➞ ✹✹This is a Warning message.✹✹
[Alert] [04-27-2017 11:08:45] [005] ➞ ✹✹This is an Alert message.✹✹

不想显示标题吗?

重要:应该在事先调用。

Printer.log.hideTitles()

不想显示日志时间吗?

默认值: false 重要: 应该提前调用。

Printer.log.hideLogsTime = true

自定义表情符号

不喜欢可用的表情符号?想设置自己的?你可以这么做。

重要:应该在事先调用。

Printer.log.successEmojiSymbol = "🎃"

其他表情符号自定义属性。

重要:应该在事先调用。

Printer.log.errorEmojiSymbol = "<SetNew>"    
Printer.log.warningEmojiSymbol = "<SetNew>"    
Printer.log.infoEmojiSymbol = "<SetNew>"    
Printer.log.alertEmojiSymbol = "<SetNew>"

自定义标题

不喜欢可用的标题?想设置自己的?想设置本地化标题?你可以这么做。

重要:应该在事先调用。

Printer.log.successLogTitle = "Hurray!!"

其他标题自定义属性。

重要:应该在事先调用。

Printer.log.errorLogTitle = "<SetNew>"    
Printer.log.warningLogTitle = "<SetNew>"    
Printer.log.infoLogTitle = "<SetNew>"    
Printer.log.alertLogTitle = "<SetNew>"

自定义符号

不喜欢可用的符号?想设置自己的?你可以这么做。

重要:应该在事先调用。

Printer.log.arrowSymbol = "⇨"

其他符号自定义属性。

重要:应该在事先调用。

Printer.log.starSymbol = "<SetNew>"

不喜欢日志中的日期格式?你也可以更改它。

默认 MM-dd-yyyy HH:mm:ss 重要 :应该在事先调用。

Printer.log.logDateFormat = "hh:mm:ss a"

将logDateFormat设置为不同格式的示例。

[✅ Success] [⌚11:12:23 AM] [🆔 001] ➞ ✹✹This is a Success message.✹✹

过滤日志:按日志类型过滤

使用过滤显示特定的日志。

Printer.log.filterLogs = [.success, .alert]

这只应打印指定类型的日志。即:成功和警告。其他所有日志将被忽略。

过滤日志:按文件过滤

是否在所有打印机日志中写入?想为了安全考虑跳过对 LoginViewController.swift的日志记录?

要跳过文件的日志记录:调用 Printer.log.skipFile();要添加文件的日志记录:调用 Printer.log.addFile()

重要 :你应该调用 addFile() 来开始打印你之前调用过 skipFile() 的相同文件的日志。这与完全禁用所有文件日志的 disable 属性不同。

禁用日志

禁用所有日志。

默认 false 重要 :您可以在任何位置设置此选项,并且它不应从设置的地方打印日志。

Printer.log.disable = true

完成块

在发生任何日志事件之前提前通知您。

重要:此块将忽略所有针对打印机的应用过滤器,即它总是会通知您任何日志,不论日志是否会打印。

    Printer.log.onLogCompletion = { (log) in
        print(log)
        //print(log.0)
    }

将返回当前日志、文件名、函数名和行号。您可以使用 log.0、log.1 等方式访问。

用例

  • 在日志事件发生前提前通知。
  • 即使您已应用过滤器,也会打印日志。
  • 调用您的 API 以存储日志信息。仅在一个地方进行编码。无依赖。

如果将 disable 设置为 true 或将 printOnlyIfDebugMode 设置为 true 以及您的应用处于 release 模式,则不会收到通知。

后台或前台日志

想查看应用何时进入后台或返回前台吗?

Printer.log.addAppEventsHandler()

[📣 INFORMATION] [⌚05-17-2017 13:17:38]  ➞ ✹✹App is in foreground now.✹✹
________________________________________________________________________________________

停止对后台或前台事件进行日志记录?

Printer.log.removeAppEventsHandler()

在检查所有日志并想知道应用进入后台或返回前台后发生了什么时很有用?

准备部署您的应用?

在 RELEASE 模式下不希望打印日志?

默认true重要:应提前调用。

Printer.log.printOnlyIfDebugMode = false

待办事项[s]

[新功能]

  • [x] 过滤日志。
  • [x] 禁用日志。
  • [x] 手动跟踪。
  • [x] 自动跟踪。
  • [x] 所有日志 - 跟踪所有日志并一起打印。
  • [x] 未来日志 – 一个将在一段时间后打印日志的功能。
  • [x] 跳过特定文件的日志。
  • [x] 委派调用以让您知道打印机已记录。
  • [x] 分别维护日志文件。
  • [x] 通过以下内容改善 README 文件:功能列表直接链接到某个特定点。
  • [x] 记录应用程序事件。例如:后台/前台事件。
  • [x] 打开 ViewController 以显示所有日志。
  • [ ] PrinterViewController 的即将推出功能。

对此类的改进有想法吗?请打开一个 问题    

致谢

Hemang Shah

您可以发送邮件 与此接触  

感谢您的支持!!

查看 贡献细节

许可证

MIT 许可证 (MIT)

阅读 LICENSE 文件以了解详细信息。