测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2017年12月 |
SwiftSwift 版本 | 4.0 |
SPM支持 SPM | ✗ |
由 Hemang Shah 维护。
对于每种类型,它都会打印一个特定的表情符号和标题,这将帮助您轻松地识别日志的确切内容。此外,它看起来也很酷。
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 [即将推出]。 参考
您可以在特定的版本中阅读变更日志文件。
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秒)
不喜欢这种花哨的日志?别担心,我们还有一个简单的日志选项。
默认值:
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.
})
}
用例
在 PrinterViewController
中查看所有打印机日志。您还可以在视图控制器内进行日志过滤。
重要:
PrinterViewController
基于Printer
的设置属性,并且工作方式完全相同,所以请确保已设置正确的属性。
用例
功能
即将推出
![]() |
![]() |
![]() |
如何使用?
如果你喜欢手动安装。
你总是可以使用 Printer 而不一定需要 PrinterViewController。但建议添加此类以获得更好的日志记录。
PrinterTableViewCell.swift
、PrinterViewController.swift
、Printer.storyboard
和 Printer.swift
添加到您的项目中。你也可以简单地添加 Printer 文件夹。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 等方式访问。
用例
如果将
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
[新功能]
对此类的改进有想法吗?请打开一个 问题。
您可以发送邮件 与此接触。
查看 贡献细节。
MIT 许可证 (MIT)
阅读 LICENSE 文件以了解详细信息。