https://www.cnblogs.com/peaker-wu/p/5624368.html
Swift 中调试状态下打印日志
首先我们应该知道Swift中真个程序的入口就是在AppDelegate.swift中。所以在打印日志在 AppDelegate.swift中是这样的
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { DLLog("你好") return true } } // 把要打印的日志写在和AppDelegate同一个等级的方法中,即不从属于AppDelegate这个类,这样在真个项目中才能使用这个打印日志,因为这就是程序的入口, //这里的T表示不指定message参数类型 func DLLog<T>(message: T, fileName: String = __FILE__, funcName: String = __FUNCTION__, lineNum : Int = __LINE__) { #if DEBUG /** * 此处还要在项目的build settings中搜索swift flags,找到 Other Swift Flags 找到Debug * 添加 -D DEBUG,即可。 */ // 1.对文件进行处理 let file = (fileName as NSString).lastPathComponent // 2.打印内容 print("[\(file)][\(funcName)](\(lineNum))\(message)") #endif }
打印的类型是:[AppDelegate.swift][application(_:didFinishLaunchingWithOptions:)](19)你好
分别是文件 方法名 行号 内容
其次,还应该处理的一点是,如图所示(其实在代码里已经说明了)
这样完整的调试状态下的log日志就完成了
func uncaughtExceptionHandler(exception: NSException){
// 异常的堆栈信息
let stackArray: NSArray = exception.callStackSymbols as NSArray
// 出现异常的原因
let reason: NSString = exception.reason as! NSString
// 异常名称
let name = exception.name
let exceptionInfo = String(format: "Exception reason:\(name) \n Excepiton name: %s \n Excepiton stack: %s",reason,stackArray)
print(exceptionInfo)
var tmpArr = NSMutableArray.init(array: stackArray)
tmpArr.insert(reason, at: 0)
//保存到本地 -- 当然你可以在下次启动的时候,上传这个log
let strLog = String(format: "%s/Documents/error.log", arguments: [NSHomeDirectory()])
let url = URL.init(fileURLWithPath: strLog)
do {
try exceptionInfo.write(to: url, atomically: true, encoding: String.Encoding.utf8)
} catch {
print(exception.description)
}
}
/////可以使用
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//////尚有问题问解决
///let observer = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())
/// NSSetUncaughtExceptionHandler { (exception) in
/// let mySelf = Unmanaged<AppDelegate>.fromOpaque(observer).takeUnretainedValue()
//// mySelf.uncaughtExceptionHandler(exception: exception)
// }
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
NSSetUncaughtExceptionHandler { exception in
// 异常的堆栈信息
let stackArray: NSArray = exception.callStackSymbols as NSArray
// 出现异常的原因
let reason: NSString = exception.reason as! NSString
// 异常名称
let name = exception.name
let exceptionInfo = String(format: "Exception reason:\(name) \n Excepiton name: %s \n Excepiton stack: %s",reason,stackArray)
print(exceptionInfo)
var tmpArr = NSMutableArray.init(array: stackArray)
tmpArr.insert(reason, at: 0)
//保存到本地 -- 当然你可以在下次启动的时候,上传这个log
let strLog = String(format: "%s/Documents/error.log", arguments: [NSHomeDirectory()])
let url = URL.init(fileURLWithPath: strLog)
do {
try exceptionInfo.write(to: url, atomically: true, encoding: String.Encoding.utf8)
} catch {
print(exception.description)
}
print(exception)
}
return true
}