自定义Swift输出日志

新建Swift文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import Foundation

public protocol logCode
{
func logChineseCode(_ level: Int) -> String
}

public func YZLOG<T>(_ message : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {

#if DEBUG
let fileName = (file as NSString).lastPathComponent

if message is Dictionary<String, Any>
{
print("\n\(fileName)第\(lineNum)行\n\((message as! Dictionary<String, Any>).logChineseCode(0))\n")
}
else if message is Array<Any>
{
print("\n\(fileName)第\(lineNum)行\n\((message as! Array<Any>).logChineseCode(0))\n")
}
if message is String
{
print("\n\(fileName)第\(lineNum)行\n\((message as! String).unicodeStr)\n")
}
else if message is CustomStringConvertible
{
print("\n\(fileName)第\(lineNum)行\n\((message as! CustomStringConvertible).description)\n")
}
else
{
print("\n\(fileName)第\(lineNum)行\n\(message)\n")
}
#endif
}


//MARK: - 重写可选型description
extension Optional: CustomStringConvertible {
public var description: String {
switch self {
case .none:
return "Optional(null)"
case .some(let obj):
if let obj = obj as? CustomStringConvertible, obj is Dictionary<String, Any> {
return "Optional:" + "\((obj as! Dictionary<String, Any>).logChineseCode(0))"
}
if let obj = obj as? CustomStringConvertible, obj is Array<Any> {
return "Optional:" + "\((obj as! Array<Any>).logChineseCode(0))"
}
return "Optional" + "(\(obj))"
}
}
}

// MARK: - 重写字典型description
extension Dictionary: logCode {
public var description: String {
var str = ""
str.append(contentsOf: "{\n")
for (key, value) in self
{
if value is String
{
let s = value as! String
str.append(contentsOf: String.init(format: "\t%@ = \"%@\",\n", key as! CVarArg, s.unicodeStr))
}
else if value is Dictionary
{
str.append(contentsOf: String.init(format: "\t%@ = \"%@\",\n", key as! CVarArg, (value as! Dictionary).description))
}
else if value is Array<Any>
{
str.append(contentsOf: String.init(format: "\t%@ = \"%@\",\n", key as! CVarArg, (value as! Array<Any>).description))
}
else
{
str.append(contentsOf: String.init(format: "\t%@ = \"%@\",\n", key as! CVarArg, "\(value)"))
}
}
str.append(contentsOf: "}")
return str
}

public func logChineseCode(_ level: Int) -> String{
var str = ""
var tab = ""
for _ in 0..<level
{
tab.append(contentsOf: "\t")
}
str.append(contentsOf: "{\n")
for (key, value) in self
{
if value is String
{
let s = value as! String
str.append(contentsOf: String.init(format: "%@\t%@ = \"%@\",\n", tab, key as! CVarArg, s.unicodeStrWith(level)))
}
else if value is Dictionary
{
str.append(contentsOf: String.init(format: "%@\t%@ = %@,\n", tab, key as! CVarArg, (value as! Dictionary).logChineseCode(level + 1)))
}
else if value is Array<Any>
{
str.append(contentsOf: String.init(format: "%@\t%@ = %@,\n", tab, key as! CVarArg, (value as! Array<Any>).logChineseCode(level + 1)))
}
else
{
str.append(contentsOf: String.init(format: "%@\t%@ = %@,\n", tab, key as! CVarArg, "\(value)"))
}
}
str.append(contentsOf: String.init(format: "%@}", tab))
return str
}
}

extension Array: logCode {
public func logChineseCode(_ level: Int) -> String
{
var str = ""
var tab = ""
str.append(contentsOf: "[\n")
for _ in 0..<level {
tab.append(contentsOf: "\t")
}
for (_, value) in self.enumerated()
{
if value is String
{
let s = value as! String
str.append(contentsOf: String.init(format: "%@\t\"%@\",\n", tab, s.unicodeStrWith(level)))
}
else if value is Dictionary<String, Any>
{
str.append(contentsOf: String.init(format: "%@\t%@,\n", tab, (value as! Dictionary<String, Any>).logChineseCode(level + 1)))
}
else if value is Array<Any>
{
str.append(contentsOf: String.init(format: "%@\t%@,\n", tab, (value as! Array<Any>).logChineseCode(level + 1)))
}
else
{
str.append(contentsOf: String.init(format: "%@\t%@,\n", tab, "\(value)"))
}
}
str.append(contentsOf: String.init(format: "%@]", tab))
return str
}

public var description: String
{
var str = ""
str.append(contentsOf: "[\n")
for (_, value) in self.enumerated()
{
if value is String
{
let s = value as! String
str.append(contentsOf: String.init(format: "\t\"%@\",\n", s.unicodeStr))
}
else if value is Dictionary<String, Any>
{
str.append(contentsOf: String.init(format: "\t%@,\n", (value as! Dictionary<String, Any>).description))
}
else if value is Array<Any>
{
str.append(contentsOf: String.init(format: "\t%@,\n", (value as! Array<Any>).description))
}
else
{
str.append(contentsOf: String.init(format: "\t%@,\n", "\(value)"))
}
}
str.append(contentsOf: "]")
return str
}
}

// MARK: - unicode转码
extension String {
func unicodeStrWith(_ level: Int) -> String {
let s = self
let data = s.data(using: .utf8)
if let data = data
{
if let id = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
{
if id is Array<Any>
{
return (id as! Array<Any>).logChineseCode(level + 1)
}
else if id is Dictionary<String, Any>
{
return (id as! Dictionary<String, Any>).logChineseCode(level + 1)
}
}
}
let tempStr1 = self.replacingOccurrences(of: "\\u", with: "\\U")
let tempStr2 = tempStr1.replacingOccurrences(of: "\"", with: "\\\"")
let tempStr3 = "\"".appending(tempStr2).appending("\"")
let tempData = tempStr3.data(using: String.Encoding.utf8)
var returnStr:String = ""
do {
returnStr = try PropertyListSerialization.propertyList(from: tempData!, options: [.mutableContainers], format: nil) as! String
}
catch
{
print(error)
}
return returnStr.replacingOccurrences(of: "\\r\\n", with: "\n")
}
var unicodeStr:String {
return self.unicodeStrWith(1)
}
}

使用方法

1
YZLOG("fsdfkjsdfs")

效果

1
2
LoginViewController.swift第27行
fsdfkjsdfs