XCGLogger Swift日志打印
2016-02-04 21:36
507 查看
Swift没有C预编译功能,所以我们不能使用调试log #define 宏定义了。 XCGLogger提供给开发者打印详细的内容到控制台。
简单举个例子吧,下面就是使用XCGLogger打印出来的结果:
介绍一下XCGLogger的使用方法:
1.CocoaPods:
添加下面的几行代码到项目的Podfile文件中。
然后运行 pod install
2.标准使用
在AppDelegate中
在上诉方法中添加一下代码
我来解释一下上面的参数:
The value for writeToFile: can be a String or NSURL. If the file already exists, it will be cleared before we use it. Omit a value or set it to nil to log to the console only. You can optionally set a different log level for the file output using the fileLogLevel parameter. Set it to nil or omit it to use the same log level as the console.
无论你想打印任何日志,你可以使用下面的遍历的方法:
3.高级使用方法
XCGLogger旨在简单快速的使用,它也允许我们提供给我们更强大更灵活地使用办法。下面是一个配置Apple System Log以及一个文件logger
的例子
自定义时间格式:
利用一个闭包(Closure)初始化XCGLogger对象
调试与发布 配置
通过使用Swift build flags,不同的log 水平能被利用到调试和发布版本。Build setting
-> Swift Compiler -> Customer Flags -> Other Swift Flags 添加-DDEBUG到the Debug entry中
后台运行打印进程
默认情况下,供给的log destination将运行在我们调用的线程中,这将确保打印的消息会被马上显示当我们调试app的时候。
然而,如果我们不想在当前线程中调试打印的结果。开发者可以特定一个 destination 进程的logs在一个分发队列中(dispatch queue)。
甚至可以这样
这种使用方法可以非常好地和 调试与发布 配置很好的结合使用,如下:
简单举个例子吧,下面就是使用XCGLogger打印出来的结果:
2014-06-09 06:44:43.600 [Debug] [AppDelegate.swift:40] application(_:didFinishLaunchingWithOptions:): Simple message
介绍一下XCGLogger的使用方法:
1.CocoaPods:
添加下面的几行代码到项目的Podfile文件中。
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks! pod 'XCGLogger', '~> 3.2'
然后运行 pod install
2.标准使用
在AppDelegate中
import XCGLogger
application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) // iOS, tvOS
在上诉方法中添加一下代码
let log = XCGLogger.defaultInstance() log.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: "path/to/file", fileLogLevel: .Debug)
我来解释一下上面的参数:
The value for writeToFile: can be a String or NSURL. If the file already exists, it will be cleared before we use it. Omit a value or set it to nil to log to the console only. You can optionally set a different log level for the file output using the fileLogLevel parameter. Set it to nil or omit it to use the same log level as the console.
无论你想打印任何日志,你可以使用下面的遍历的方法:
log.verbose("A verbose message, usually useful when working on a specific problem") log.debug("A debug message") log.info("An info message, probably useful to power users looking in console.app") log.warning("A warning message, may indicate a possible error") log.error("An error occurred, but it's recoverable, just info about what happened") log.severe("A severe error occurred, we are likely about to crash now")
3.高级使用方法
XCGLogger旨在简单快速的使用,它也允许我们提供给我们更强大更灵活地使用办法。下面是一个配置Apple System Log以及一个文件logger
的例子
// Create a logger object with no destinations let log = XCGLogger(identifier: "advancedLogger", includeDefaultDestinations: false) // Create a destination for the system console log (via NSLog) let systemLogDestination = XCGNSLogDestination(owner: log, identifier: "advancedLogger.systemLogDestination") // Optionally set some configuration options systemLogDestination.outputLogLevel = .Debug systemLogDestination.showLogIdentifier = false systemLogDestination.showFunctionName = true systemLogDestination.showThreadName = true systemLogDestination.showLogLevel = true systemLogDestination.showFileName = true systemLogDestination.showLineNumber = true systemLogDestination.showDate = true // Add the destination to the logger log.addLogDestination(systemLogDestination) // Create a file log destination let fileLogDestination = XCGFileLogDestination(owner: log, writeToFile: "/path/to/file", identifier: "advancedLogger.fileLogDestination") // Optionally set some configuration options fileLogDestination.outputLogLevel = .Debug fileLogDestination.showLogIdentifier = false fileLogDestination.showFunctionName = true fileLogDestination.showThreadName = true fileLogDestination.showLogLevel = true fileLogDestination.showFileName = true fileLogDestination.showLineNumber = true fileLogDestination.showDate = true // Process this destination in the background fileLogDestination.logQueue = XCGLogger.logQueue // Add the destination to the logger log.addLogDestination(fileLogDestination) // Add basic app info, version info etc, to the start of the logs log.logAppDetails()
自定义时间格式:
let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM/dd/yyyy hh:mma" dateFormatter.locale = NSLocale.currentLocale() log.dateFormatter = dateFormatter
利用一个闭包(Closure)初始化XCGLogger对象
let log: XCGLogger = {
let log = XCGLogger.defaultInstance()
log.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: nil, fileLogLevel: .Debug)
let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM/dd/yyyy hh:mma" dateFormatter.locale = NSLocale.currentLocale() log.dateFormatter = dateFormatter
return log
}()
调试与发布 配置
通过使用Swift build flags,不同的log 水平能被利用到调试和发布版本。Build setting
-> Swift Compiler -> Customer Flags -> Other Swift Flags 添加-DDEBUG到the Debug entry中
#if DEBUG log.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: nil) #else log.setup(.Severe, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: nil) #endif
后台运行打印进程
默认情况下,供给的log destination将运行在我们调用的线程中,这将确保打印的消息会被马上显示当我们调试app的时候。
然而,如果我们不想在当前线程中调试打印的结果。开发者可以特定一个 destination 进程的logs在一个分发队列中(dispatch queue)。
fileLogDestination.logQueue = XCGLogger.logQueue
甚至可以这样
fileLogDestination.logQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)
这种使用方法可以非常好地和 调试与发布 配置很好的结合使用,如下:
#if DEBUG log.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: nil) #else log.setup(.Severe, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: nil) if let consoleLog = log.logDestination(XCGLogger.Constants.baseConsoleLogDestinationIdentifier) as? XCGConsoleLogDestination { consoleLog.logQueue = XCGLogger.logQueue } #endif
相关文章推荐
- UIColor之扩展Swift
- Swift 教學:如何使用iOS Charts API 製作漂亮的圖表
- swift如何实现语言国际化,适应各国语言
- swift 通知
- swift获取屏幕分辨率
- swift 单例模式正确写法
- oc 调用 swift Demo
- Swift语言指南--可选值
- swift创建一个空项目
- swift 函数 做参数,函数做返回值
- swift构造方法
- Swift_UINavigationController
- swift调用OC语法
- iOS开发用swift添加admob广告,iosswiftadmob广告
- SWIFT学习笔记01
- swift懒加载
- swift菜鸟入门视频教程-07-闭包
- Swift类与结构、存储属性、计算属性、函数与方法、附属脚本等
- Swift: 是用Custom Segue还是用Transition动画
- Swift: 是用Custom Segue还是用Transition动画