中文
什么是JLConsoleLog?
JLConsoleLog 是一款强大的App内建工具,可以用来帮助Swift开发者在开发和生产环境下打印log信息。在解bug时,你不会在非调试环境下错过关键和有用的log。你也可以把他集成到你的后门调试工具中,它能帮助你解决很多棘手的问题。
如何去操作它?
JLConsoleLog支持3种显示模式——Floating, Bubble 和 FullScreen。
在浮动模式下在选项栏上有4个按钮。第一个按钮是设置按钮,里面包含清空所有log,过滤分类和等级的选项。第二个按钮用来切换浮动和全屏模式。点击第三个按钮,控制台会变成一个半透明的浮动气泡,并只显示错误和警告数量。最后一个是关闭按钮。
浮动控制台会在无操作的5秒后自动变成半透明模式。此外,你可以拖动浮动控制台和气泡来避免遮挡。
当你点击每条log的cell时,就会进入log的详情页面。
性能监控是一个新的功能,你可以通过点击气泡按钮获取监控图表。支持监控CPU、内存和帧率。
如何在工程中使用?
** JLConsoleController** 是开放给开发者的控制台控制器。它包括一个共享的实例。你可以通过它来设置style(展示模式)和logEnable(开启log)。当你设置它的style时,会根据你给定的模式立即显示。像这样:
if JLConsoleController.shared.style == .Hidden {
JLConsoleController.shared.style = .Floating //show console in floating fashion
} else {
JLConsoleController.shared.style = .Hidden //hide console
}
当logEnable为true的时候,控制台才会收集log,反之亦然。
JLConsoleController.shared.logEnabled = true
这里提供了一些列类型的日志。
func JLLevelLog(level: JLConsoleLogLevel, category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
func JLVerboseLog( category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
func JLDebugLog( category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
func JLInfoLog( category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
func JLWarningLog( category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
func JLErrorLog( category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
** JLConsoleLogLevel**是一个枚举,列举了一系列的等级。警告和错误的数量会被展示在选项栏和气泡上。
** JLConsoleLogCategory** 是一个String的别名,用来给你需要打log的业务进行分类。你可以自定义自己的Category来满足你的需求,例如视频、页面追踪、商品详情等等。如果你需要可以过滤Category,你必须用这种方式注册:
let SubPageTestLog:JLConsoleLogCategory = "com.consolelog.mybusiness" //declare a category
JLConsoleController.shared.register(newCategory: SubPageTestLog) //register it
参数contextData是一个可被序列化的字典。其数据会被在详情页中以Json的形式展现。
参数formats 是一个variadic参数型的String。其第一个参数值会被当做cell的标题展示在控制台中。
如果needPrint为true,在调试环境下log的信息会被打印在Xcode的控制台中。
除此之外,JLConsoleController提供了followingAction闭包,可以在使用JLConsoleLog记录日志后进行其他的操作。例如,你可以在followingAction闭包进行Firebase或者友盟等埋点追踪。 同时,不要忘了你需要在记录log时,将hasFollowingAction设为true。
JLErrorLog(category: SubPageTestLog, hasFollowingAction: true ,needPrint: true, contextData: ["test":5], formats: "Error!",#function,String(#line))
上面是以一个错误日志为例。
性能监控
JLConsoleLog提供了一个性能监控。你可以这样开启它。
JLConsoleController.shared.performanceMonitable = true
此项目的Github地址是https://github.com/jacklandrin/JLConsoleLog
需求
- iOS 12
- Swift 5.2
- Xcode 11
引用
原文地址
https://www.jacklandrin.com/2020/04/28/jlconsolelog/
English
What is JLConsoleLog?
JLConsoleLog is an awesome tool In-App to help swift developer log information in both development and production environment. You won’t miss any key and useful logs about the bugs in non-debug mode. You also can integrate it in your project’s backdoor toolkit, it will help you solve vital issues.
How to operate it?
JLConsoleLog supports three types of style (display mode) — Floating, Bubble and FullScreen
There are four buttons on the option view of floating mode. The first button is setting button where you can clear all logs from this console, filter categories and levels. The second one is for switching between floating and fullscreen mode. If the third one is pressed, the console will be a translucent bubble that only displays warning and error count. The last one is close button.
The floating console could become translucent automatically after 5s, if you don’t touch it. Additionally, you can drag floating console and bubble to anywhere to avoid disturb you.
While you tap a log cell, you can enter the detail page of log.
Performance monitor is a new function. You can invoke a monitor chart from bubble button now. It supports CPU, Memory and FPS.
How to use it in your project?
The JLConsoleController is the console’s controller opened for developers. It contains a shared instance. You could set style and logEnabled via it. While you set its style (display mode), the console will immediately show in terms of you given. Like this:
if JLConsoleController.shared.style == .Hidden {
JLConsoleController.shared.style = .Floating //show console in floating fashion
} else {
JLConsoleController.shared.style = .Hidden //hide console
}
If logEnabled is true, the console will collect log data, vice verse.
JLConsoleController.shared.logEnabled = true
A series functions are offered to log.
func JLLevelLog(level: JLConsoleLogLevel, category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
func JLVerboseLog( category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
func JLDebugLog( category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
func JLInfoLog( category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
func JLWarningLog( category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
func JLErrorLog( category: JLConsoleLogCategory, hasFollowingAction:Bool = false, needPrint:Bool = false, contextData:Dictionary<String,Any> , formats: String...)
The JLConsoleLogLevel is an enum to sort by different levels. The numbers of warning and error are displayed on option view and bubble.
The JLConsoleLogCategory is your business category and is an alias of String. You can define your own categories met your demand, such as Video, TrackPage, Commodity Detail… If you need to filter your categories, you must register in this way in your code:
let SubPageTestLog:JLConsoleLogCategory = "com.consolelog.mybusiness" //declare a category
JLConsoleController.shared.register(newCategory: SubPageTestLog) //register it
The parameter, contextData, is a serializable Dictionary. The data will be shown on the detail page in Json.
The parameters, formats, is variadic parameters of String. The first value will be shown on the cell’s title in console.
If needPrint equals true, the log information will print in your Xcode console in Debug environment.
Otherwise, JLConsoleController provides a followingAction to operate other actions when you finish logging. For example, you can send a track point log to statistics server such as Firebase in followingAction closure. Meanwhile, please don’t forget to set hasFollowingAction as true while you log.
JLErrorLog(category: SubPageTestLog, hasFollowingAction: true ,needPrint: true, contextData: ["test":5], formats: "Error!",#function,String(#line))
This is an error log example.
Performance Monitor
JLConsoleLog provides a performance monitor. You can add these to turn on it.
JLConsoleController.shared.performanceMonitable = true
The git address is https://github.com/jacklandrin/JLConsoleLog
Requirement
- iOS 12
- Swift 5.2
- Xcode 11