测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可 | MIT |
发布上次发布 | 2017年8月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Drew Kiino 维护。
Atlantis
是我创建的一个非常强大的日志框架,用于日常使用,包括为企业级开发、初创公司或快速原型设计 API。它是 无类型 的,这意味着您可以传入任何东西,从 字符串
到您自己制作的定制 对象
……并且它将基本 美化打印 该对象内的所有值或一般任何事物。它具有极佳的可读性和易用性。
将此添加到您的 Podfile 中...
# for Swift 3.0
pod 'Atlantis'
# for Swift 2.3
pod 'Atlantis', :git => 'https://github.com/DrewKiino/Atlantis.git', :branch => 'swift2.3'
然后执行 pod install,搞定!
这包括记录的 日期
、源
、函数
、行号
的戳记,以及值的 实际 类型
。
注意:日期的格式为:月、日、年、小时,然后是每日时间。
// I suggest initializing this variable in the global instance
let log = Atlantis.Logger()
// Let's log some stuff
log.verbose("Hello, World!")
log.info("Hello, World!")
log.warning("Hello, World!")
log.debug("Hello, World!")
log.error("Hello, World!")
它会打印以下内容...
Atlantis
被构建来 美化打印
任何事物。
正确的文本对齐
Atlantis
会根据其之前的对齐方式自动对其日志进行对齐。但是,您可以通过设置以下变量来更改此配置:
Atlantis.Configuration.alignmentThreshold
它默认为 5
。
可选
Atlantis
会安全地展开任何可选。
let doIExist: String? = nil
log.warning(doIExist)
// prints 'nil'
空字符串
Atlantis
会以视觉方式打印空字符串。
let emptyString: String = ""
log.warning(emptyString)
// prints ""
原生类型
let string = "Hello, World"
let int = 123
let double = 12.3
let float = 12.3
let bool = true
// you can either log one value
log.debug(string)
// or all of them like so,
log.debug(string, int, double, float, bool)
// prints
"Hello, World" // first one
"Hello, World" // prints each with a new line
123
12.3
12.3
true
数组
// array of ints
let numbers = [123, 1234, 12345]
log.debug(numbers)
// prints
[
123,
1234,
12345
]
// lets try arrays of arrays
let numberArray = [[1, 2, 3], [4, 5]]
log.debug(numberArray)
// prints
[
[
1,
2,
3
],
[
4,
5
]
]
字典
// on to dictionaries...
let dictionary: [String: AnyObject] = [
"quote": "It is better to have loved, than to have never loved at all?"
]
log.debug(dictionary)
// prints
[
"quote": "It is better to have loved, than to have never loved at all?"
]
// how about NSDictionary types?
var dictionary = NSDictionary()
dictionary.updateValue("will this work?", key: "question")
log.debug(dictionary)
// prints
[
"question": "will this work?"
]
// say we got two response objects from the server,
// now both objects are the same but one of them has missing data...
responses.map { log.debug($0) }
// prints
{
"response": "Here is some data!",
"success" 200
},
{
"response": null,
"success" 200
}
// Atlantis will print all of the object's keys regardless of missing
// or empty values and will print null if need be.
对象
// now let's get to the fun part,
// native Foundation (ex: UIView, UIColor, etc.)
log.debug(UIColor())
// prints
<UIPlaceholderColor: 0x7ff1fb517ab0>
// native NSObjects
public class Dog: NSObject {
var name = "Doug"
}
let dog = Dog()
log.debug(dog)
// prints
{
"name": "Doug"
}
// But what about objects you created with no native subclasses?
public class CustomObject {
var id: Int = 123
var name: String = "Ben"
}
let customObject = CustomObject()
log.debug(customObject)
// prints
{
"id": 123,
"name": "Doug"
}
// Haha, no way?
// Alright, well how about custom objects with custom objects in them?
public class ParentObject {
var id: Int = 456
var name: String = "Tammy"
var customObject: CustomObject = CustomObject()
}
let parentObject = ParentObject()
log.debug(parentObject)
// prints
{
"id": 456,
"name": "Tammy"
"customObject": {
"id": 123,
"name": "Doug"
}
}
// That's right.
// Okay, custom objects with an array of custom objects. ;)
public class ParentObject {
var id: Int = 456
var name: String = "Tammy"
var customObjects: [CustomObject] = [CustomObject(), CustomObject()]
}
let parentObject = ParentObject()
log.debug(parentObject)
// prints
{
"id": 456,
"name": "Tammy"
"customObjects": [
{
"id": 123,
"name": "Doug"
},
{
"id": 123,
"name": "Doug"
}
]
}
// Not impressed?
let parentObject1 = ParentObject()
let parentObject2 = ParentObject() // one of its child has a dictionary
let parents: [ParentObject] = [parentObject1, parentObject2]
log.debug(parents)
// prints
[
{
"id": 456,
"name": "Tammy"
"customObjects": [
{
"id": 123,
"name": "Doug"
},
{
"id": 123,
"name": "Doug"
}
]
},
{
"id": 456,
"name": "Tammy"
"customObjects": [
{
"id": 123,
"name": "Doug"
},
{
"id": 123,
"name": "Doug"
"dictionary": [
"likes": "baseball",
"dislikes": "pad thai"
]
}
]
}
]
// Atlantis' logging is infinitely and ambiguously recursive,
// it supports almost all data types including arrays, dictionaries,
// and any objects within any objects. 👍🏼
结构体
// Great!! Now on to some more stand-alone but much needed types.
struct Struct {
var name: String = "Bob the Builder"
var skills: [String] = ["structures, buildings"]
}
log.debug(Struct())
// prints
{
"skills" : [
"structures, buildings"
],
"name" : "Bob the Builder"
}
枚举
enum ErrorType {
case Severe
case Moderate
case Casual
}
let type: ErrorType = .Severe
log.debug(type)
// prints
Severe
// one more example.
log.debug(ErrorType.Moderate)
log.debug(ErrorType.Casual)
// prints
Moderate
Casual
Atlantis
将像这样打印所有错误:
Error: [ViewController.swift/viewDidLoad()/line:98]
{
"code" : 404,
"localizedDescription" : "The operation couldn’t be completed. (Hello, World! error 404.)",
"domain" : "Hello, World!",
"userInfo" : {
"error": "found"
"note": "syntax"
}
}
它将自动解析本地化描述、错误代码、域和用户信息来自 NSError
对象。
Atlantis.Configuration.highlightsErrors // default false
默认情况下,Atlantis
会以白色或启用彩色日志时以彩色打印所有日志。但是,如果您启用错误高亮显示,它将始终突出显示错误,而不管任何设置参数。
Atlantis.Configuration.filteredErrorCodes
Atlantis
有能力根据错误代码过滤错误。例如,您有一个向网络发送请求的方法,并使其只能一次发送一个请求,因此它会始终取消最后一个请求。然而,有些API不受我们控制,会发送错误而无需您的许可。
假设您想过滤出错误代码 -1099 // 离线错误
,
Atlantis.Configuration.filteredErrorCodes.append(-1099)
// let's call a method that throws errors, however one of the
// errors is something we want to filter out.
method() { error in
log.error(error) // can either be error 404 or -1099?
}
// will only print the error if the error code is 404
现在,如果方法抛出 -1099
错误,Atlantis
将会跳过它!
点按是 Atlantis
扩展,允许您像常规打印一样打印,但会返回输入的值。
func add(x: Int, _ y: Int) -> Int { return x + y }
let addXY = log.tap.debug(add(3, 5))
// prints 8 and assigns the value to addXY
像 .Verbose
等普通扩展也位于 .Tap
下。
Promises
兼容更具体地使用 PromiseKit...
func promise() -> Promise<String> {
return Promise { fulfill, reject in
// blah blah
fulfill("Hello from server!")
}
}
promise()
.then { log.tap($0) }
.then { reply in
// blah blah
}
.catch { log.error($0) }
// prints "Hello from server!" while completing the promise.
请注意,.Tap
只能接受单个输入。
Atlantis.Configuration.logLevel // default .Verbose
五个日志级别是:详细
、信息
、警告
、调试
、错误
和 无
,按优先级排序。
例如,如果您将日志级别设置为 调试
,Atlantis
将只会打印级别为 调试
和 错误
的日志。
将日志级别设置为 无
意味着 Atlantis
将跳过所有日志执行。我建议在将应用程序部署到生产时使用此设置。
Atlantis.Configuration.showExtraInfo // default true
您还可以通过将此参数设置为 false 来隐藏源详细信息。
Atlantis
能够提供全彩色定制,
// colors
Atlantis.Configuration.hasColoredLogs // default false
Atlantis.Configuration.hasWhiteBackground // default false
Atlantis.Configuration.coloredLogLevels // default [.Verbose, .Info, .Warning, .Debug, .Error]
// using a Tuple initializer
Atlantis.Configuration.logColors.info = Atlantis.XCodeColor(fg: (Int, Int, Int)>, bg: <(Int, Int, Int)>)
// using UIColor setting only the foreground
Atlantis.Configuration.logColors.info = Atlantis.XCodeColor(fg: UIColor)
// or using UIColor setting both the foreground and background
Atlantis.Configuration.logColors.debug = Atlantis.XCodeColor(fg: UIColor, bg: UIColor)
默认情况下,Atlantis
不在彩色中打印日志。如果您想打印彩色,您需要在此处设置启动时的配置。
但是,为了启用日志颜色,您首先必须下载 xcode 包管理器 Alcatraz 并在 Xcode 中启用它。然后拉起包管理器,并安装 XCodeColors。
MIT 许可证 (MIT)
版权所有 © 2015 Andrew Aquino http://drewkiino.github.io/
在此特此授予任何获取本软件和相关文档文件(统称为“软件”)副本的人免费使用权,可以在不受限制的情况下处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,以及允许向其提供软件的人这样做,但须遵守以下条件:
上述版权声明和本许可声明应包含在软件的所有副本或实质部分。
软件按“现状”提供,无论明示或默示,均不提供任何形式的保证,包括但不限于适销性、适用于特定目的和不受侵犯的保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论是由合同、侵权或其他行为引起的,以及从软件或其使用或任何其他操作中产生的职责。